Enumerable#find

Would it make sense to allow #find to take an argument or a block
(but not both)?

I’d like this

x.find {|a| a==y }

to be shortened to

x.find(y)

The only question this raises in my mind is: Do we assume == or === or
what? I would prefer/expect ==.

Synonyms and related methods (#detect, #find_all, …) would be
similarly affected.

Comments??

Hal

Hi,

···

At Fri, 16 Jan 2004 22:29:04 +0900, Hal Fulton wrote:

Would it make sense to allow #find to take an argument or a block
(but not both)?

It conflicts current spec, which takes the value when nothing
found. You would need another name.


Nobu Nakada

I don’t understand. Do you mean this has changed in 1.8?

find traditionally has returned nil when it fails.

Can you give me an example?

Thanks much,

Hal

···

nobu.nokada@softhome.net wrote:

Hi,

At Fri, 16 Jan 2004 22:29:04 +0900, > Hal Fulton wrote:

Would it make sense to allow #find to take an argument or a block
(but not both)?

It conflicts current spec, which takes the value when nothing
found. You would need another name.

Hi,

Would it make sense to allow #find to take an argument or a block
(but not both)?

It conflicts current spec, which takes the value when nothing
found. You would need another name.

I don’t understand. Do you mean this has changed in 1.8?

find traditionally has returned nil when it fails.

Sorry, it was wrong a little. Enumerable#find calls the
optional parameter `ifnone’ and returns its result, when no
object matches. In 1.6 or former, nil returned regardless of
the result.

Can you give me an example?

$ ruby -e ‘p [1,2,3].find(proc{puts “not found”; true}){|x|x.zero?}’
not found
true

···

At Fri, 16 Jan 2004 22:50:38 +0900, Hal Fulton wrote:


Nobu Nakada

nobu.nokada@softhome.net schrieb im Newsbeitrag
news:200401161401.i0GE1C2N006185@sharui.nakada.kanuma.tochigi.jp…

Hi,

Would it make sense to allow #find to take an argument or a block
(but not both)?

It conflicts current spec, which takes the value when nothing
found. You would need another name.

I don’t understand. Do you mean this has changed in 1.8?

find traditionally has returned nil when it fails.

Sorry, it was wrong a little. Enumerable#find calls the
optional parameter `ifnone’ and returns its result, when no
object matches. In 1.6 or former, nil returned regardless of
the result.

Can you give me an example?

$ ruby -e ‘p [1,2,3].find(proc{puts “not found”; true}){|x|x.zero?}’
not found
true

Apart from that, many cases of arr.find {|x| x==i} can be dealt with by
arr.include?( i ) - at least those where one does not need the element
returned but just the information whether it’s there or not. But I guess,
you asked because of the other cases… :slight_smile:

Regards

robert
···

At Fri, 16 Jan 2004 22:50:38 +0900, > Hal Fulton wrote: