object loops and what they return

E

Eric Mahurin

Consider these loops:

<object>.<loop-method> { <code> }

where loop method is each, each_with_index, upto, downto, step,
and probably others.

Although it is not documented, all of these look to return the
origninal object (collection or int). Does anybody find this
useful?? If not, I would propose that these return nil just
like loop, while, until, begin/end while, and begin/end until.
I've never found the return value of these methods useful, but
I have found the the built-in loops returning nil useful. Here
are a couple:

# find first index where you find the object obj in array
index = array.each_with_index do |i,x|
break(i) if obj.equal?(x)
end

# find last index where you find the object obj in array
index = (array.size-1).downto(0) do |i,x|
break(i) if obj.equal?(x)
end

The problem with the above now is that index will be the loop
object (array and array.size-1 from above) when you don't find
the obj. Instead of the above, I end up using old-style loops
to accomplish what I want.

With "each" returning nil, you can also see that many of the
derived loops in Enumerable become trival almost to where you
don't need them.




Yahoo! Mail
Stay connected, organized, and protected. Take the tour:
http://tour.mail.yahoo.com/mailtour.html
 
R

Robert Klemme

Eric Mahurin said:
Consider these loops:

<object>.<loop-method> { <code> }

where loop method is each, each_with_index, upto, downto, step,
and probably others.

Although it is not documented, all of these look to return the
origninal object (collection or int). Does anybody find this
useful?? If not, I would propose that these return nil just
like loop, while, until, begin/end while, and begin/end until.
I've never found the return value of these methods useful, but
I have found the the built-in loops returning nil useful. Here
are a couple:

# find first index where you find the object obj in array
index = array.each_with_index do |i,x|
break(i) if obj.equal?(x)
end

# find last index where you find the object obj in array
index = (array.size-1).downto(0) do |i,x|
break(i) if obj.equal?(x)
end
a=%w{a b c d e f g ab c} => ["a", "b", "c", "d", "e", "f", "g", "ab", "c"]
a.index "c" => 2
a.rindex "c" => 8
a.index "foo"
=> nil
The problem with the above now is that index will be the loop
object (array and array.size-1 from above) when you don't find
the obj. Instead of the above, I end up using old-style loops
to accomplish what I want.

With "each" returning nil, you can also see that many of the
derived loops in Enumerable become trival almost to where you
don't need them.

Interesting aspect. I assume the return behavior is from a time where break
could not return a value so your constructions weren't possible.

Typically I put such functionality into methods and then I use "return" to
short circuit:

module Enumerable
def find_pos(x)
each_with_index {|e,i| return i if x == e}
nil
end

def find_cond
each_with_index {|e,i| return i if yield e}
nil
end
end

Kind regards

robert
 
B

Brian Schröder

Consider these loops:
=20
<object>.<loop-method> { <code> }
=20
where loop method is each, each_with_index, upto, downto, step,
and probably others.
=20
Although it is not documented, all of these look to return the
origninal object (collection or int). Does anybody find this
useful?? [snip]

I use it occasionally for doing evil method chaining. Somewhere I
heard it is evil, but sometimes its nice.

So your proposal indeed allows for some nifty operations.

regards,

Brian


--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
D

David A. Black

Hi --

Consider these loops:

<object>.<loop-method> { <code> }

where loop method is each, each_with_index, upto, downto, step,
and probably others.

Although it is not documented, all of these look to return the
origninal object (collection or int).

I think the return values are documented in ri for all of these.

[...]
With "each" returning nil, you can also see that many of the
derived loops in Enumerable become trival almost to where you
don't need them.

On the other hand, if you were to do this (looking for nil):

obj = array.each {|e| break(e) if e.nil? }

and each returned nil, you wouldn't know whether you'd succeeded or
not.

The various find and include? and any? and index methods are probably
your best bet for this kind of operation.


David
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top