From: Robert Klemme Date: 2011-05-11T06:25:29+09:00 Subject: Re: Enumerable#find returns an enumerator? On 10.05.2011 22:33, Lars Schirrmeister wrote: > The find method on an array takes a block and returns an enumerator > if no block is given. The value you pass to the find method is not > the value you want to find but the value which is returned if no > block evaluates to true. More precisely: the value passed is something which returns the replacement value when #called: irb(main):007:0> (10..20).to_a.find(99) {|x| x > 150} NoMethodError: undefined method `call' for 99:Fixnum from (irb):7:in `find' from (irb):7 from /usr/local/bin/irb19:12:in `
' irb(main):008:0> (10..20).to_a.find(lambda {99}) {|x| x > 150} => 99 Which is precisely what the docs say: $ ri19 -T Enumerable#find -------------------------------------------------------- Enumerable#find enum.detect(ifnone = nil) {| obj | block } => obj or nil enum.find(ifnone = nil) {| obj | block } => obj or nil From Ruby 1.9.1 ------------------------------------------------------------------------ Passes each entry in _enum_ to _block_. Returns the first for which _block_ is not +false+. If no object matches, calls _ifnone_ and returns its result when it is specified, or returns +nil+ (1..10).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> nil (1..100).detect {|i| i % 5 == 0 and i % 7 == 0 } #=> 35 There are places where Ruby's documentation is bad but here documentation is clear. Why do so many people tell different stories if it is so easy to read this up in documentation? (And, btw, it has been that way in all versions from 1.8.6 on.) Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/