From: Eric Mahurin Date: 2005-05-12T08:01:14+09:00 Subject: Re: object loops and what they return > > Consider these loops: > > > > . { } > > > > 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 > > original object (collection or int). > > I think the return values are documented in ri for all of > these. Enumerable says nothing about the return value or each. Array.each says it returns an Array, but not what's in that Array. Before testing, I was thinking it might be the same as map/collect. The integer loops also say they return an integer, but not what it is. > > 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. Then you do it another way. Differentiating between the element nil in a collection and nil meaning nothing is a common problem, I think. You have the same problem using find: obj = array.find{|e| not e.kind_of?Integer } If you were looking for the first non-integer in array and it happened to be nil, you couldn't tell whether nil was that object or there were no non-integers. > The various find and include? and any? and index methods are > probably > your best bet for this kind of operation. In the particular code I was looking at, none of them worked (I think I looked at all of them). I needed a different compare (equal?) and in one case I wanted to go in reverse order. Discover Yahoo! Use Yahoo! to plan a weekend, have fun online and more. Check it out! http://discover.yahoo.com/