Make Object#singleton_methods work correctly for singleton classes of objects
Fixes [Bug #10901]
This commit is contained in:
parent
11662c70b0
commit
9aba971e42
3
class.c
3
class.c
@ -1447,6 +1447,9 @@ rb_obj_singleton_methods(int argc, const VALUE *argv, VALUE obj)
|
|||||||
int recur = TRUE;
|
int recur = TRUE;
|
||||||
|
|
||||||
if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
|
if (rb_check_arity(argc, 0, 1)) recur = RTEST(argv[0]);
|
||||||
|
if (RB_TYPE_P(obj, T_CLASS) && FL_TEST(obj, FL_SINGLETON)) {
|
||||||
|
rb_singleton_class(obj);
|
||||||
|
}
|
||||||
klass = CLASS_OF(obj);
|
klass = CLASS_OF(obj);
|
||||||
origin = RCLASS_ORIGIN(klass);
|
origin = RCLASS_ORIGIN(klass);
|
||||||
me_arg.list = st_init_numtable();
|
me_arg.list = st_init_numtable();
|
||||||
|
@ -865,6 +865,29 @@ class TestObject < Test::Unit::TestCase
|
|||||||
assert_match(/@\u{3046}=6\b/, x.inspect)
|
assert_match(/@\u{3046}=6\b/, x.inspect)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_singleton_methods
|
||||||
|
assert_equal([], Object.new.singleton_methods)
|
||||||
|
assert_equal([], Object.new.singleton_methods(false))
|
||||||
|
c = Class.new
|
||||||
|
def c.foo; end
|
||||||
|
assert_equal([:foo], c.singleton_methods - [:yaml_tag])
|
||||||
|
assert_equal([:foo], c.singleton_methods(false))
|
||||||
|
assert_equal([], c.singleton_class.singleton_methods(false))
|
||||||
|
c.singleton_class.singleton_class
|
||||||
|
assert_equal([], c.singleton_class.singleton_methods(false))
|
||||||
|
|
||||||
|
o = c.new.singleton_class
|
||||||
|
assert_equal([:foo], o.singleton_methods - [:yaml_tag])
|
||||||
|
assert_equal([], o.singleton_methods(false))
|
||||||
|
o.singleton_class
|
||||||
|
assert_equal([:foo], o.singleton_methods - [:yaml_tag])
|
||||||
|
assert_equal([], o.singleton_methods(false))
|
||||||
|
|
||||||
|
c.extend(Module.new{def bar; end})
|
||||||
|
assert_equal([:bar, :foo], c.singleton_methods.sort - [:yaml_tag])
|
||||||
|
assert_equal([:foo], c.singleton_methods(false))
|
||||||
|
end
|
||||||
|
|
||||||
def test_singleton_class
|
def test_singleton_class
|
||||||
x = Object.new
|
x = Object.new
|
||||||
xs = class << x; self; end
|
xs = class << x; self; end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user