mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* proc.c (mnew): generate method object that wraps method_missing,
when #respond_to_missing? is defined. * test/ruby/test_object.rb (test_respond_to_missing): add test suites for #respond_to_missing? changes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25073 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
399dace5ed
commit
c4b0b4c91c
3 changed files with 62 additions and 0 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Thu Sep 24 13:32:53 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* proc.c (mnew): generate method object that wraps method_missing,
|
||||||
|
when #respond_to_missing? is defined.
|
||||||
|
|
||||||
|
* test/ruby/test_object.rb (test_respond_to_missing): add test
|
||||||
|
suites for #respond_to_missing? changes.
|
||||||
|
|
||||||
Thu Sep 24 09:41:42 2009 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
Thu Sep 24 09:41:42 2009 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||||
|
|
||||||
* lib/mathn.rb (Bignum#**): Fixed bignum**fixnum that was broken when
|
* lib/mathn.rb (Bignum#**): Fixed bignum**fixnum that was broken when
|
||||||
|
|
21
proc.c
21
proc.c
|
@ -883,6 +883,19 @@ rb_obj_is_method(VALUE m)
|
||||||
return rb_typeddata_is_kind_of(m, &method_data_type);
|
return rb_typeddata_is_kind_of(m, &method_data_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
missing_wrap(VALUE dummy, VALUE args, int argc, VALUE *argv)
|
||||||
|
{
|
||||||
|
VALUE new_args = rb_ary_new4(argc, argv);
|
||||||
|
VALUE obj = RARRAY_PTR(args)[0];
|
||||||
|
VALUE sym = RARRAY_PTR(args)[1];
|
||||||
|
|
||||||
|
|
||||||
|
rb_ary_unshift(new_args, sym);
|
||||||
|
return rb_funcall2(obj, rb_intern("method_missing"),
|
||||||
|
check_argc(RARRAY_LEN(new_args)), RARRAY_PTR(new_args));
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
|
mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
|
||||||
{
|
{
|
||||||
|
@ -896,6 +909,14 @@ mnew(VALUE klass, VALUE obj, ID id, VALUE mclass, int scope)
|
||||||
again:
|
again:
|
||||||
me = rb_method_entry(klass, id);
|
me = rb_method_entry(klass, id);
|
||||||
if (UNDEFINED_METHOD_ENTRY_P(me)) {
|
if (UNDEFINED_METHOD_ENTRY_P(me)) {
|
||||||
|
ID rmiss = rb_intern("respond_to_missing?");
|
||||||
|
VALUE sym = ID2SYM(id);
|
||||||
|
|
||||||
|
if (!rb_method_basic_definition_p(klass, rmiss)) {
|
||||||
|
if (RTEST(rb_funcall(obj, rmiss, 1, sym))) {
|
||||||
|
return rb_proc_new(missing_wrap, rb_assoc_new(obj, sym));
|
||||||
|
}
|
||||||
|
}
|
||||||
rb_print_undef(klass, id, 0);
|
rb_print_undef(klass, id, 0);
|
||||||
}
|
}
|
||||||
def = me->def;
|
def = me->def;
|
||||||
|
|
|
@ -304,6 +304,39 @@ class TestObject < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_respond_to_missing
|
||||||
|
c = Class.new
|
||||||
|
c.class_eval do
|
||||||
|
def respond_to_missing?(id)
|
||||||
|
if id == :foobar
|
||||||
|
true
|
||||||
|
else
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def method_missing(id,*args)
|
||||||
|
if id == :foobar
|
||||||
|
return [:foo, *args]
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
foo = c.new
|
||||||
|
assert_equal([:foo], foo.foobar);
|
||||||
|
assert_equal([:foo, 1], foo.foobar(1));
|
||||||
|
assert(foo.respond_to?(:foobar))
|
||||||
|
assert_equal(false, foo.respond_to?(:foobarbaz))
|
||||||
|
assert_raise(NoMethodError) do
|
||||||
|
foo.foobarbaz
|
||||||
|
end
|
||||||
|
|
||||||
|
foobar = foo.method(:foobar)
|
||||||
|
assert_equal([:foo], foobar.call);
|
||||||
|
assert_equal([:foo, 1], foobar.call(1));
|
||||||
|
end
|
||||||
|
|
||||||
def test_send_with_no_arguments
|
def test_send_with_no_arguments
|
||||||
assert_raise(ArgumentError) { 1.send }
|
assert_raise(ArgumentError) { 1.send }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue