A better way to do this job?

topics.each do |f|
  times +=1
  puts f.title
  if times > 4
    break

a little ugly, doesn't it?

···

--
Posted via http://www.ruby-forum.com/.

Alle Tuesday 10 February 2009, Zhenning Guan ha scritto:

topics.each do |f|
  times +=1
  puts f.title
  if times > 4
    break

a little ugly, doesn't it?

If topics is an array, you can use:

topics[0..4].each{|f| puts f.title}

Another option, which can be used for any Enumerable object is:

topics.each_with_index do |f, i|
  puts f.title
  break if i > 4
end

While not as nice as the first version, it's a bit clearer than your code.

I hope this helps

Stefano

topics[0,5].each do |f|
   puts f.title
end

# if you need the variable "times" to have the same value as above:
# times = topics[0,5].size

Jesus.

···

On Tue, Feb 10, 2009 at 10:11 AM, Zhenning Guan <g.zhen.ning@gmail.com> wrote:

topics.each do |f|
times +=1
puts f.title
if times > 4
   break

a little ugly, doesn't it?

there are a lot of ways. My first reaction is:

   topics.first(5).each do |f|
     puts f.title
   end

If you define to_s on the topic class to return title then you can do:

   puts topics.first(5)

and be done with it.

···

On Feb 10, 2009, at 01:11 , Zhenning Guan wrote:

topics.each do |f|
times +=1
puts f.title
if times > 4
   break

Zhenning Guan wrote:

topics.each do |f|
  times +=1
  puts f.title
  if times > 4
    break

Another option, which can be used for any Enumerable object is:

topics.each_with_index do |f, i|
  puts f.title
  break if i > 4
end

While not as nice as the first version, it's a bit clearer than your
code.

In my opinion, it's nicer than the first version because it doesn't have
to create a sub array in memory, like all the solutions posted so far
do, which could be a problem with large arrays and large slices.

4.times do |i|
  puts topics[i].title
end

···

--
Posted via http://www.ruby-forum.com/\.

Your code also won't work. Your tempvar is not declared and you have no end to the block.

topics[0..3].each do |topic|
puts topic
end

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

···

On 10/02/2009, at 8:11 PM, Zhenning Guan <g.zhen.ning@gmail.com> wrote:

topics.each do |f|
times +=1
puts f.title
if times > 4
   break

a little ugly, doesn't it?
--
Posted via http://www.ruby-forum.com/\.

Assuming there is a times=0 before this, and what you want to do is
print each topic title if there are 5 or less topics, but only those 5
if there are more, then:

topics[0..4].each {|f| puts f.title}

or:

topics.each_with_index do |f, i|
  puts f.title
  break if i > 4
end

is probably the best way to do it (the first is, IMO, cleaner and more
understandable, the latter seems like it would somewhat more efficient
because it doesn't create an auxiliary array, though for this size it
doesn't matter, it conceivably might for very large arrays.)

5.times {|i| puts topics[i].title}

works if there are 5 or more topics, but will dump nils at the end if
there are less than 5.

···

On Tue, Feb 10, 2009 at 1:11 AM, Zhenning Guan <g.zhen.ning@gmail.com> wrote:

topics.each do |f|
times +=1
puts f.title
if times > 4
   break

a little ugly, doesn't it?

First doesn't take an argument I'm ruby. It does
If you're using rails enumerable mixin

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

···

On 10/02/2009, at 10:30 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:

On Feb 10, 2009, at 01:11 , Zhenning Guan wrote:

topics.each do |f|
times +=1
puts f.title
if times > 4
  break

there are a lot of ways. My first reaction is:

topics.first(5).each do |f|
   puts f.title
end

If you define to_s on the topic class to return title then you can do:

puts topics.first(5)

and be done with it.

Slices and 'sub-arrays' aren't copies of the objects, they simply refer to the identical objects.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

···

On 10/02/2009, at 10:37 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

Zhenning Guan wrote:

topics.each do |f|
times +=1
puts f.title
if times > 4
   break

Another option, which can be used for any Enumerable object is:

topics.each_with_index do |f, i|
puts f.title
break if i > 4
end

While not as nice as the first version, it's a bit clearer than your
code.

In my opinion, it's nicer than the first version because it doesn't have
to create a sub array in memory, like all the solutions posted so far
do, which could be a problem with large arrays and large slices.

4.times do |i|
puts topics[i].title
end

--
Posted via http://www.ruby-forum.com/\.

Hi --

In my opinion, it's nicer than the first version because it doesn't have
to create a sub array in memory, like all the solutions posted so far
do, which could be a problem with large arrays and large slices.

Ryan had something like:

   topics.first(5).each do |t|
     puts t.title
   end

which, I believe, does not create an intermediate array because
topics.first(5) returns an enumerator.

David

···

On Tue, 10 Feb 2009, 7stud -- wrote:

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

http://www.wishsight.com => Independent, social wishlist management!

mentarbation.

it isn't a problem until it is a problem.

···

On Feb 10, 2009, at 03:37 , 7stud -- wrote:

In my opinion, it's nicer than the first version because it doesn't have
to create a sub array in memory, like all the solutions posted so far
do, which could be a problem with large arrays and large slices.

Hi --

···

On Tue, 10 Feb 2009, Julian Leviston wrote:

First doesn't take an argument I'm ruby. It does
If you're using rails enumerable mixin

$ ruby19 -ve 'puts [1,2,3,4].first(2)'
ruby 1.9.1p0 (2009-01-30 revision 21907) [i686-linux]
1
2

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

http://www.wishsight.com => Independent, social wishlist management!

Hi --

···

On Tue, 10 Feb 2009, Julian Leviston wrote:

Slices and 'sub-arrays' aren't copies of the objects, they simply refer to the identical objects.

The same can be said of any array; the objects inside it exist (in
many cases, at least) already. Still, container objects do take up
memory. If you don't believe it, try this:

   ruby -e 'a = [:a,:b,:c]; b = ; while true; b << a[0,3]; end'

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

http://www.wishsight.com => Independent, social wishlist management!

Would you please stop top posting? It's just bad manners.

James Edward Gray II

···

On Feb 10, 2009, at 6:21 AM, Julian Leviston wrote:

First doesn't take an argument I'm ruby. It does
If you're using rails enumerable mixin

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

Hi --

···

On Tue, 10 Feb 2009, David A. Black wrote:

Hi --

On Tue, 10 Feb 2009, 7stud -- wrote:

In my opinion, it's nicer than the first version because it doesn't have
to create a sub array in memory, like all the solutions posted so far
do, which could be a problem with large arrays and large slices.

Ryan had something like:

topics.first(5).each do |t|
   puts t.title
end

which, I believe, does not create an intermediate array because
topics.first(5) returns an enumerator.

Rewind. As Tom points out, first(x) returns an array. (Which in fact
makes sense, since it's often used entirely on its own.)

David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2\)

http://www.wishsight.com => Independent, social wishlist management!

Julian Leviston wrote:

Slices and 'sub-arrays' aren't copies of the objects, they simply
refer to the identical objects.

Is it your opinion that an array of pointers takes up no memory? Which
takes up more memory: an array of int's or an array of pointers?

···

--
Posted via http://www.ruby-forum.com/\.

David showed it in 1.9... but I had to point this out:

% ruby -ve 'puts [1,2,3,4].first(2)'
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
1
2

this goes back to at least 1.7 iirc. #first was introduced in 1.6 and 1.7 gave it an optional length arg.

···

On Feb 10, 2009, at 04:21 , Julian Leviston wrote:

First doesn't take an argument I'm ruby. It does
If you're using rails enumerable mixin

ruby -e 'a = [:a,:b,:c]; b = ; while true; b << a[0,3]; end'

That's a little bit unfair though. If you pushed an infinite number of
object of whatever class, you'd eventually run out of memory.

I personally find #first() rather confusing (BTW [1,2,3].first
(3).class => Array). I'd rather prefer [0..4] (although [0,5] should
perform slightly better) or maybe #each_index, which doesn't create an
intermediary array. #each_index would also have the advantage that it
expects only an Enumerable.

Well, an array of Fixnums takes up the same space as an array of 'pointers'
to Fixnums, since Fixnums are immediate.

···

On Tue, Feb 10, 2009 at 10:28 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

Julian Leviston wrote:
> Slices and 'sub-arrays' aren't copies of the objects, they simply
> refer to the identical objects.
>

Is it your opinion that an array of pointers takes up no memory? Which
takes up more memory: an array of int's or an array of pointers?

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale

agreed!

···

On Feb 10, 2009, at 05:59 , James Gray wrote:

On Feb 10, 2009, at 6:21 AM, Julian Leviston wrote:

First doesn't take an argument I'm ruby. It does
If you're using rails enumerable mixin

Would you please stop top posting? It's just bad manners.