Feature #7394 ยป enumerable_find_noncallable.patch
enum.c | ||
---|---|---|
*
|
||
* Passes each entry in <i>enum</i> to <em>block</em>. Returns the
|
||
* first for which <em>block</em> is not false. If no
|
||
* object matches, calls <i>ifnone</i> and returns its result when it
|
||
* is specified, or returns <code>nil</code> otherwise.
|
||
* object matches, and <i>ifnone</i> is callable, then it is called
|
||
* and its result is returned. If there are no matches, and
|
||
* <i>ifnone</i> is not callable, then <i>ifnone</i> is returned.
|
||
* Returns <code>nil</code> otherwise.
|
||
*
|
||
* If no block is given, an enumerator is returned instead.
|
||
*
|
||
... | ... | |
return memo->u1.value;
|
||
}
|
||
if (!NIL_P(if_none)) {
|
||
return rb_funcall(if_none, rb_intern("call"), 0, 0);
|
||
if (rb_respond_to(if_none, rb_intern("call"))) {
|
||
return rb_funcall(if_none, rb_intern("call"), 0, 0);
|
||
}
|
||
return if_none;
|
||
}
|
||
return Qnil;
|
||
}
|
test/ruby/test_enum.rb | ||
---|---|---|
assert_equal(2, @obj.find {|x| x % 2 == 0 })
|
||
assert_equal(nil, @obj.find {|x| false })
|
||
assert_equal(:foo, @obj.find(proc { :foo }) {|x| false })
|
||
assert_equal(:bar, @obj.find(:bar) {|x| false })
|
||
end
|
||
def test_find_index
|
||
-
|
enum.c | ||
---|---|---|
return memo->u1.value;
|
||
}
|
||
if (!NIL_P(if_none)) {
|
||
if (rb_respond_to(if_none, rb_intern("call"))) {
|
||
if (rb_check_funcall(if_none, rb_intern("call"), 0, 0) != Qundef) {
|
||
return rb_funcall(if_none, rb_intern("call"), 0, 0);
|
||
}
|
||
return if_none;
|
||
-
|
enum.c | ||
---|---|---|
return memo->u1.value;
|
||
}
|
||
if (!NIL_P(if_none)) {
|
||
if (rb_check_funcall(if_none, rb_intern("call"), 0, 0) != Qundef) {
|
||
return rb_funcall(if_none, rb_intern("call"), 0, 0);
|
||
}
|
||
VALUE result = rb_check_funcall(if_none, rb_intern("call"), 0, 0);
|
||
if (result != Qundef) return result;
|
||
return if_none;
|
||
}
|
||
return Qnil;
|