[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

a better way to do this job?

Zhenning Guan

2/10/2009 9:11:00 AM

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


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

32 Answers

Stefano Crocco

2/10/2009 9:16:00 AM

0

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


Jesús Gabriel y Galán

2/10/2009 9:19:00 AM

0

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?

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.

Ryan Davis

2/10/2009 11:30:00 AM

0


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.


7stud --

2/10/2009 11:38:00 AM

0

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

Julian Leviston

2/10/2009 12:17:00 PM

0

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.ze...
Learn rails: http://sensei.ze...

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

Julian Leviston

2/10/2009 12:22:00 PM

0

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

Blog: http://random8.ze...
Learn rails: http://sensei.ze...

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

Julian Leviston

2/10/2009 12:30:00 PM

0

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

Blog: http://random8.ze...
Learn rails: http://sensei.ze...

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

David A. Black

2/10/2009 12:45:00 PM

0

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

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

David A. Black

2/10/2009 12:52:00 PM

0

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

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

David A. Black

2/10/2009 12:54:00 PM

0

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.r...
Coming in 2009: The Well-Grounded Rubyist (http://manning....)

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