Forum: Ruby a better way to do this job?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ffd881829651935d986c52d12279befb?d=identicon&s=25 Zhenning Guan (rubyernewbie)
on 2009-02-10 10:12
topics.each do |f|
  times +=1
  puts f.title
  if times > 4
    break


a little ugly, doesn't it?
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2009-02-10 10:17
(Received via mailing list)
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
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-02-10 10:22
(Received via mailing list)
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.
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-02-10 12:32
(Received via mailing list)
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.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-10 12:39
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
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-10 13:19
(Received via mailing list)
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/
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-10 13:25
(Received via mailing list)
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/
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-10 13:31
(Received via mailing list)
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/
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-10 13:46
(Received via mailing list)
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!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-10 13:53
(Received via mailing list)
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!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-10 13:59
(Received via mailing list)
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!
9b905791cbdbb1af35b65e02c3217e23?d=identicon&s=25 Tom Link (Guest)
on 2009-02-10 14:16
(Received via mailing list)
>    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.
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-02-10 15:01
(Received via mailing list)
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/

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

James Edward Gray II
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-10 15:05
(Received via mailing list)
Hi --

On Tue, 10 Feb 2009, Tom Link 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 :-)


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!
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-10 15:07
(Received via mailing list)
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!
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-10 16:29
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?
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-02-10 17:35
(Received via mailing list)
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?
>


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
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-02-10 21:08
(Received via mailing list)
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


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.
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-02-10 21:10
(Received via mailing list)
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.


agreed!
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-02-10 21:12
(Received via mailing list)
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.

mentarbation.

it isn't a problem until it is a problem.
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-10 21:15
(Received via mailing list)
Hi --

On Wed, 11 Feb 2009, Ryan Davis 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 :-)


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!
D7463bd611f227cfb2ef4da4a978a203?d=identicon&s=25 Christopher Dicely (Guest)
on 2009-02-11 03:03
(Received via mailing list)
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?


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.
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-02-11 10:50
(Received via mailing list)
David A. Black wrote:
> 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.

Ruby arrays are copy-on-write though. So subarray =
my_array[100...1000100]
does *not* take up any memory except for the additional array object
(which
will contain a pointer to the 100th item of my_array and the length
1000000).

HTH,
Sebastian
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2009-02-11 14:02
(Received via mailing list)
Hi --

On Wed, 11 Feb 2009, Sebastian Hungerecker wrote:

>
> Ruby arrays are copy-on-write though. So subarray = my_array[100...1000100]
> does *not* take up any memory except for the additional array object (which
> will contain a pointer to the 100th item of my_array and the length 1000000).

My memory-eating example was probably unrealistic. Maybe I'm too
steeped in the idea that enumerators are good because they don't
create intermediate arrays.... (I have some real issues with some of
what they do, so I tend to cling to that one.)

Ryan's point (it's not a problem until it's a problem) is a key one,
in any case. I definitely would not discourage creation of
intermediate objects unless there's a real reason to avoid them.


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!
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-11 16:30
(Received via mailing list)
Sorry dude. Imust have been having an odd day. I swear my irb said it
don't work :)

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-11 16:31
(Received via mailing list)
What makes it bad manners? I'm simply posting where my cursor appears.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
9b905791cbdbb1af35b65e02c3217e23?d=identicon&s=25 Tom Link (Guest)
on 2009-02-11 17:11
(Received via mailing list)
> What makes it bad manners?

Many people consider it an annoyance. IIRC the netiquette RFC even has
a section on quoting: http://www.ietf.org/rfc/rfc1855.txt
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-02-11 18:31
(Received via mailing list)
Hi Julian,

Am Donnerstag, 12. Feb 2009, 00:29:38 +0900 schrieb Julian Leviston:
>>
>> On Feb 10, 2009, at 05:59 , James Gray wrote:
>>> Would you please stop top posting?  It's just bad manners.
>
> What makes it bad manners? I'm simply posting where my cursor appears.

That's the point. It's not where the reader's eyes appear. What is
easier to move: Your cursor or some dozens of pairs of eyes?

Bertram
1bc63d01bd3fcccc36fb030a62039352?d=identicon&s=25 David Masover (Guest)
on 2009-02-11 19:04
(Received via mailing list)
Bertram Scharpf wrote:
>
> That's the point. It's not where the reader's eyes appear. What is
> easier to move: Your cursor or some dozens of pairs of eyes?
>

Agreed. It also makes it that much more difficult to get context. While
this is an extreme example, notice how naturally that reads, to someone
coming late to the discussion?

It's also a bit irritating how you're spamming your blog (and "learn
rails") in something like a signature -- but there's nothing separating
it from your post, and it's also right there before whatever you're
quoting.
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-12 02:15
(Received via mailing list)
It's not possible to bottom post while maintaining my signatures on
the iPhone as far as I can tell. Sorry.

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-02-12 05:27
(Received via mailing list)
Hi Julian,

Am Donnerstag, 12. Feb 2009, 10:13:31 +0900 schrieb Julian Leviston:
>>> What makes it bad manners?
>>
>> Many people consider it an annoyance. IIRC the netiquette RFC even has
>> a section on quoting: http://www.ietf.org/rfc/rfc1855.txt
>
> It's not possible to bottom post while maintaining my signatures on the
> iPhone as far as I can tell. Sorry.

It's not possible for - drum roll - you! Make a decision what
should be made easy: writing for you or reading for the people you
like to impress. I case you decide making it easier for yourself,
wait for the audience you comb out by that.

Sorry.

Bertram
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-12 06:44
(Received via mailing list)
Doesn't the quoting line indicate enough who wrote what?

I find your english unintelligible.

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

On 12/02/2009, at 3:25 PM, Bertram Scharpf <lists@bertram-scharpf.de>
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-02-12 12:43
(Received via mailing list)
Hi Julian,

Am Donnerstag, 12. Feb 2009, 14:42:42 +0900 schrieb Julian Leviston:
>>
>> It's not possible for - drum roll - you! Make a decision what
>> should be made easy: writing for you or reading for the people you
>> like to impress. I case you decide making it easier for yourself,
>> wait for the audience you comb out by that.
>
> Doesn't the quoting line indicate enough who wrote what?
>
> I find your english unintelligible.

Sorry, I didn't guess how difficult it is for you to accept such a
simple thing. I won't try to argue any further.

Bertram
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2009-02-12 20:09
(Received via mailing list)
On Feb 12, 2009, at 03:41 , Bertram Scharpf wrote:

> Am Donnerstag, 12. Feb 2009, 14:42:42 +0900 schrieb Julian Leviston:
>> I find your english unintelligible.
>
> Sorry, I didn't guess how difficult it is for you to accept such a
> simple thing. I won't try to argue any further.

Please stop feeding the troll.
This topic is locked and can not be replied to.