A better way to do this job?


#1

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

a little ugly, doesn’t it?


#2

Alle Tuesday 10 February 2009, Zhenning G. 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


#3

On Tue, Feb 10, 2009 at 10:11 AM, Zhenning G. removed_email_address@domain.invalid
wrote:

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

a little ugly, doesn’t it?

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.


#4

On Feb 10, 2009, at 01:11 , Zhenning G. 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.


#5

Zhenning G. 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


#6

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/


#7

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/


#8

Hi –

On Tue, 10 Feb 2009, Julian L. 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!


#9

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/


#10

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.

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!


#11

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.


#12

Hi –

On Tue, 10 Feb 2009, Julian L. 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!


#13

Hi –

On Tue, 10 Feb 2009, Tom L. wrote:

  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’m just making the point that even though two arrays may contain the
same objects (not copies), arrays are not cost-free, so creating
intermediate arrays may well be an issue.

I personally find #first() rather confusing (BTW [1,2,3].first
(3).class => Array).

Yes, that’s right – I’ve over-enumeratored my response :slight_smile:

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!


#14

On Feb 10, 2009, at 6:21 AM, Julian L. 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/

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

James Edward G. II


#15

Julian L. 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?


#16

Hi –

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

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!


#17

On Tue, Feb 10, 2009 at 10:28 AM, 7stud – removed_email_address@domain.invalid
wrote:

Julian L. 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?

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


Rick DeNatale

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


#18

On Feb 10, 2009, at 04:21 , Julian L. wrote:

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

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.


#19

On Feb 10, 2009, at 05:59 , James G. wrote:

On Feb 10, 2009, at 6:21 AM, Julian L. 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.

agreed!


#20

Hi –

On Wed, 11 Feb 2009, Ryan D. wrote:

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.

I’ve clearly got 1.9 on the brain :slight_smile:

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!