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.
Zhenning G. (Guest)
on 2009-02-10 11:12
topics.each do |f|
  times +=1
  puts f.title
  if times > 4
    break


a little ugly, doesn't it?
Stefano C. (Guest)
on 2009-02-10 11:17
(Received via mailing list)
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
Jesús Gabriel y Galán (Guest)
on 2009-02-10 11:22
(Received via mailing list)
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.
Ryan D. (Guest)
on 2009-02-10 13:32
(Received via mailing list)
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.
7stud -. (Guest)
on 2009-02-10 13:39
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
Julian L. (Guest)
on 2009-02-10 14: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/
Julian L. (Guest)
on 2009-02-10 14: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/
Julian L. (Guest)
on 2009-02-10 14: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/
David A. Black (Guest)
on 2009-02-10 14:46
(Received via mailing list)
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!
David A. Black (Guest)
on 2009-02-10 14:53
(Received via mailing list)
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!
David A. Black (Guest)
on 2009-02-10 14: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!
Tom L. (Guest)
on 2009-02-10 15: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.
James G. (Guest)
on 2009-02-10 16:01
(Received via mailing list)
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
David A. Black (Guest)
on 2009-02-10 16:05
(Received via mailing list)
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 :-)


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!
David A. Black (Guest)
on 2009-02-10 16: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!
7stud -. (Guest)
on 2009-02-10 17:29
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?
Rick D. (Guest)
on 2009-02-10 18:35
(Received via mailing list)
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
Ryan D. (Guest)
on 2009-02-10 22:08
(Received via mailing list)
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.
Ryan D. (Guest)
on 2009-02-10 22:10
(Received via mailing list)
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!
Ryan D. (Guest)
on 2009-02-10 22: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.
David A. Black (Guest)
on 2009-02-10 22:15
(Received via mailing list)
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 :-)


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!
Christopher D. (Guest)
on 2009-02-11 04:03
(Received via mailing list)
On Tue, Feb 10, 2009 at 1: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?


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.
Sebastian H. (Guest)
on 2009-02-11 11:50
(Received via mailing list)
David A. Black wrote:
> 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.

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
David A. Black (Guest)
on 2009-02-11 15:02
(Received via mailing list)
Hi --

On Wed, 11 Feb 2009, Sebastian H. 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!
Julian L. (Guest)
on 2009-02-11 17: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/
Julian L. (Guest)
on 2009-02-11 17: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/
Tom L. (Guest)
on 2009-02-11 18: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
Bertram S. (Guest)
on 2009-02-11 19:31
(Received via mailing list)
Hi Julian,

Am Donnerstag, 12. Feb 2009, 00:29:38 +0900 schrieb Julian L.:
>>
>> On Feb 10, 2009, at 05:59 , James G. 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
David M. (Guest)
on 2009-02-11 20:04
(Received via mailing list)
Bertram S. 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.
Julian L. (Guest)
on 2009-02-12 03: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/
Bertram S. (Guest)
on 2009-02-12 06:27
(Received via mailing list)
Hi Julian,

Am Donnerstag, 12. Feb 2009, 10:13:31 +0900 schrieb Julian L.:
>>> 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
Julian L. (Guest)
on 2009-02-12 07: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 S. <removed_email_address@domain.invalid>
Bertram S. (Guest)
on 2009-02-12 13:43
(Received via mailing list)
Hi Julian,

Am Donnerstag, 12. Feb 2009, 14:42:42 +0900 schrieb Julian L.:
>>
>> 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
Ryan D. (Guest)
on 2009-02-12 21:09
(Received via mailing list)
On Feb 12, 2009, at 03:41 , Bertram S. wrote:

> Am Donnerstag, 12. Feb 2009, 14:42:42 +0900 schrieb Julian L.:
>> 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.