Object loops and what they return

Yes. Not often, but occasionally the return value is useful.

For example, there was a solution like this posted to the rails mailing list, on how to iterate over all the items in a record, or print out a 'no records found' type line if none existed:

if my_array.each do |item|
   puts "item"
end.empty?
   puts "(no items found)"
end

···

________________________________

From: Eric Mahurin [mailto:eric_mahurin@yahoo.com]
Sent: Wed 5/11/2005 3:31 PM
To: ruby-talk ML
Subject: Re: object loops and what they return

> 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

I was wanting to compare the objects with equal? (compares
object ids) not == (what index/rindex use).

> 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

The loops I describe above were in some method. I didn't want
to create a new method just for those loops. That would be
kind of silly. I used traditional loops instead. Also, with
what I am proposing you wouldn't need the "nil" in the above 2
methods.

Does anybody find a use for these loop methods returning the
original object (undocumented) instead of nil (like other
loops)? If not, I would like to make an RCR for this.

__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail

I've posted similar code to this list a few days ago. See ruby-talk:140818
Sorry for duplicating.

>> .each do |e|
>> end.any? or "empty!"
#=> "empty!"
>> [1].each do |e|
>> end.any? or puts "empty!"
#=> true

···

On 5/12/05, Gavin Kistner <Gavin.Kistner@refinery.com> wrote:

Yes. Not often, but occasionally the return value is useful.

For example, there was a solution like this posted to the rails mailing list, on how to iterate over all the items in a record, or print out a 'no records found' type line if none existed:

if my_array.each do |item|
   puts "item"
end.empty?
   puts "(no items found)"
end

________________________________

From: Eric Mahurin [mailto:eric_mahurin@yahoo.com]
Sent: Wed 5/11/2005 3:31 PM
To: ruby-talk ML
Subject: Re: object loops and what they return

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

I was wanting to compare the objects with equal? (compares
object ids) not == (what index/rindex use).

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

The loops I describe above were in some method. I didn't want
to create a new method just for those loops. That would be
kind of silly. I used traditional loops instead. Also, with
what I am proposing you wouldn't need the "nil" in the above 2
methods.

Does anybody find a use for these loop methods returning the
original object (undocumented) instead of nil (like other
loops)? If not, I would like to make an RCR for this.

__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail

--
http://nohmad.sub-port.net