enumerable_find_noncallable.patch

Zachary Scott, 11/19/2012 12:36 PM

Download (3.45 KB)

View differences:

enum.c
193 193
 *
194 194
 *  Passes each entry in <i>enum</i> to <em>block</em>. Returns the
195 195
 *  first for which <em>block</em> is not false.  If no
196
 *  object matches, calls <i>ifnone</i> and returns its result when it
197
 *  is specified, or returns <code>nil</code> otherwise.
196
 *  object matches, and <i>ifnone</i> is callable, then it is called
197
 *  and its result is returned.  If there are no matches, and
198
 *  <i>ifnone</i> is not callable, then <i>ifnone</i> is returned.
199
 *  Returns <code>nil</code> otherwise.
198 200
 *
199 201
 *  If no block is given, an enumerator is returned instead.
200 202
 *
......
217 219
	return memo->u1.value;
218 220
    }
219 221
    if (!NIL_P(if_none)) {
220
	return rb_funcall(if_none, rb_intern("call"), 0, 0);
222
      if (rb_respond_to(if_none, rb_intern("call"))) {
223
         return rb_funcall(if_none, rb_intern("call"), 0, 0);
224
      }
225
      return if_none;
221 226
    }
222 227
    return Qnil;
223 228
}
test/ruby/test_enum.rb
81 81
    assert_equal(2, @obj.find {|x| x % 2 == 0 })
82 82
    assert_equal(nil, @obj.find {|x| false })
83 83
    assert_equal(:foo, @obj.find(proc { :foo }) {|x| false })
84
    assert_equal(:bar, @obj.find(:bar) {|x| false })
84 85
  end
85 86

  
86 87
  def test_find_index
87
- 
enum.c
219 219
	return memo->u1.value;
220 220
    }
221 221
    if (!NIL_P(if_none)) {
222
      if (rb_respond_to(if_none, rb_intern("call"))) {
222
      if (rb_check_funcall(if_none, rb_intern("call"), 0, 0) != Qundef) {
223 223
         return rb_funcall(if_none, rb_intern("call"), 0, 0);
224 224
      }
225 225
      return if_none;
226
- 
enum.c
219 219
	return memo->u1.value;
220 220
    }
221 221
    if (!NIL_P(if_none)) {
222
      if (rb_check_funcall(if_none, rb_intern("call"), 0, 0) != Qundef) {
223
         return rb_funcall(if_none, rb_intern("call"), 0, 0);
224
      }
222
      VALUE result = rb_check_funcall(if_none, rb_intern("call"), 0, 0);
223
      if (result != Qundef) return result;
225 224
      return if_none;
226 225
    }
227 226
    return Qnil;
228
-