enum #find ifnone parameter can now be non-callable #186

Closed
wants to merge 3 commits into
from

Projects

None yet

3 participants

@mrmargolis

In trunk the Enumerable #find method ifnone parameter has to be callable or nil. I found that sometimes I want to return a simple value without wrapping it in a proc. This pull request adds support for non-callable defaults when no items match.

a = [1, 2, 3]

#The current behavior
a.find(proc { :foo }) { |x| x > 3 }
=> :foo

#Possible with my branch
a.find(0) { |x| x > 3 }
=> 0
@nobu
nobu commented on a095eb7 Sep 24, 2012

you can use `rb_check_funcall()'.

@mrmargolis

@nobu Let me know if that was not what you were suggesting. I am new to editing ruby :)

@nobu
Member
nobu commented Oct 5, 2012

rb_check_funcall() calls the method and returns the result if it is callable, and returns Qundef if the method is not defined.

VALUE result = rb_check_funcall(...);
if (result != Qundef) return result;
return ifnone;
@mrmargolis

Thanks @nobu. I have implemented your approach.

@zzak
Member
zzak commented Nov 19, 2012

Closing this, please refer to Feature #7394 in redmine

@zzak zzak closed this Nov 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment