a better way to do this job?

Z

Zhenning Guan

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


a little ugly, doesn't it?
 
S

Stefano Crocco

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
 
J

Jesús Gabriel y Galán

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.
 
R

Ryan Davis

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

7stud --

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

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.title
end
 
J

Julian Leviston

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/

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

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.title
end
 
D

David A. Black

Hi --

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!
 
D

David A. Black

Hi --

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!
 
D

David A. Black

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

--
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!
 
T

Tom Link

=A0 =A0ruby -e 'a =3D [:a,:b,:c]; b =3D []; 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 =3D> 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.
 
D

David A. Black

Hi --

Hi --



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!
 
7

7stud --

Julian said:
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?
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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.
 
R

Ryan Davis

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.
 
R

Ryan Davis

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.

mentarbation.

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

David A. Black

Hi --

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.

I've clearly got 1.9 on the brain :)


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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top