Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4196

Re: Enumerable#find returns an enumerator?

Path csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!news.dougwise.org!gegeweb.org!talisker.lacave.net!lacave.net!not-for-mail
From Jeremy Bopp <jeremy@bopp.net>
Newsgroups comp.lang.ruby
Subject Re: Enumerable#find returns an enumerator?
Date Tue, 10 May 2011 14:12:48 -0500
Organization Service de news de lacave.net
Lines 37
Message-ID <4DC98E2C.1010705@bopp.net> (permalink)
References <6cc262653e3f003816a98ce87fb22fdf@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1305054785 76900 65.111.164.187 (10 May 2011 19:13:05 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Tue, 10 May 2011 19:13:05 +0000 (UTC)
In-Reply-To <6cc262653e3f003816a98ce87fb22fdf@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 382906
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <4DC98E2C.1010705@bopp.net>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:4196

Show key headers only | View raw


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

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Enumerable#find returns an enumerator? Roger Pack <rogerpack2005@gmail.com> - 2011-05-10 13:48 -0500
  Re: Enumerable#find returns an enumerator? Chris Hulan <chris.hulan@gmail.com> - 2011-05-10 11:56 -0700
  Re: Enumerable#find returns an enumerator? Jeremy Bopp <jeremy@bopp.net> - 2011-05-10 14:12 -0500
  Re: Enumerable#find returns an enumerator? Lars Schirrmeister <l.schirrmeister@gmx.de> - 2011-05-10 15:33 -0500
    Re: Enumerable#find returns an enumerator? Robert Klemme <shortcutter@googlemail.com> - 2011-05-10 23:23 +0200
      Re: Enumerable#find returns an enumerator? Brian Candler <b.candler@pobox.com> - 2011-05-11 03:43 -0500
        Re: Enumerable#find returns an enumerator? Robert Klemme <shortcutter@googlemail.com> - 2011-05-11 05:54 -0500
          Re: Enumerable#find returns an enumerator? Brian Candler <b.candler@pobox.com> - 2011-05-11 07:47 -0500
            Re: Enumerable#find returns an enumerator? Robert Klemme <shortcutter@googlemail.com> - 2011-05-11 19:13 +0200
            Re: Enumerable#find returns an enumerator? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-11 15:08 -0500
              Re: Enumerable#find returns an enumerator? Brian Candler <b.candler@pobox.com> - 2011-05-13 03:20 -0500
  Re: Enumerable#find returns an enumerator? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-10 15:39 -0500
    Re: Enumerable#find returns an enumerator? Ryan Davis <ryand-ruby@zenspider.com> - 2011-05-10 18:13 -0500

csiph-web