8000 enum #find ifnone parameter can now be non-callable by mrmargolis · Pull Request #186 · ruby/ruby · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and 8000 the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,10 @@ find_i(VALUE i, VALUE memop, int argc, VALUE *argv)
*
* 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.
*
Expand All @@ -217,7 +219,9 @@ enum_find(int argc, VALUE *argv, VALUE obj)
return memo->u1.value;
}
if (!NIL_P(if_none)) {
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;
}
Expand Down
1 change: 1 addition & 0 deletions test/ruby/test_enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def test_find
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
Expand Down
0