On 5/10/2011 13:48, Roger Pack wrote:
> Hello all.
> I would have expected that
> 
> [1,2,3].find(3)
> 
> just return me the element 3, not an enumerator, since it will at most
> return me one item, so it doesn't seem very "enumerable" to me (at most
> one item).

Take a look at the documentation:

http://rdoc.info/stdlib/core/1.9.2/Enumerable:find

Basically, you're trying to use the method incorrectly.  I'm not sure if
what you're trying to do is find the third element in the array or find
all the 3's within the array.  Maybe what you actually want is this:

[1,2,3][2]

That will return the third element from the array.  In this case 3.

If you want to return all the 3's for some reason, keep in mind that
there may be more than 1 or even none of them in any given array.
Writing your code to account for 3 different return cases from find
would be messy and error prone, so always returning an enumerable would
make sense for most users since that is the most general case.  It may
be an enumerable of 0 or 1 items, but that's still a valid enumeration.

Of course, the way you seem to be going about this won't work anyway
since the method doesn't work the way you expect. :-)  To find all the
3's do this as Chris suggested:

[1,2,3].find { |x| x == 3 }

-Jeremy