A better way to do this job?

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.

On Tue, Feb 10, 2009 at 1:11 AM, Zhenning G. [email protected]
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.

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

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 (The Well-Grounded Rubyist)

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

Sorry dude. Imust have been having an odd day. I swear my irb said it
don’t work :slight_smile:

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

What makes it bad manners? I’m simply posting where my cursor appears.

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

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

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

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/

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

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. [email protected]

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

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.

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.