Enumerable#find

H

Hal Fulton

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
 
N

nobu.nokada

Hi,

At Fri, 16 Jan 2004 22:29:04 +0900,
Hal said:
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.
 
H

Hal Fulton

Hi,

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



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.

Can you give me an example?


Thanks much,

Hal
 
N

nobu.nokada

Hi,

At Fri, 16 Jan 2004 22:50:38 +0900,
Hal said:
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
 
R

Robert Klemme

Hi,

At Fri, 16 Jan 2004 22:50:38 +0900,
Hal said:
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... :)

Regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top