a better way to do this job?

C

Christopher Dicely

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

works if there are 5 or more topics, but will dump nils at the end if
there are less than 5.
 
S

Sebastian Hungerecker

David said:
Hi --



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
 
D

David A. Black

Hi --

David said:
Hi --



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

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

Julian Leviston

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/

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

Bertram Scharpf

Hi Julian,

Am Donnerstag, 12. Feb 2009, 00:29:38 +0900 schrieb Julian Leviston:
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
 
D

David Masover

Bertram said:
Hi Julian,

Am Donnerstag, 12. Feb 2009, 00:29:38 +0900 schrieb Julian Leviston:


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

Bertram Scharpf

Hi Julian,

Am Donnerstag, 12. Feb 2009, 10:13:31 +0900 schrieb Julian Leviston:
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
 
B

Bertram Scharpf

Hi Julian,

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

Ryan Davis

Am Donnerstag, 12. Feb 2009, 14:42:42 +0900 schrieb Julian Leviston:

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.
 

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