diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | NEWS | 2 | ||||
-rw-r--r-- | object.c | 9 | ||||
-rw-r--r-- | test/ruby/test_class.rb | 6 |
4 files changed, 23 insertions, 0 deletions
@@ -1,3 +1,9 @@ +Thu Aug 8 23:01:20 2013 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * object.c (rb_mod_singleton_p): new method Module#singleton_class? to + return whether the receiver is a singleton class or not. + [ruby-core:51087] [Feature #7609] + Thu Aug 8 21:56:44 2013 Tanaka Akira <akr@fsij.org> * time.c (time_overflow_p): Avoid signed integer overflow. @@ -41,6 +41,8 @@ with all sufficient information, see the ChangeLog file. * New methods: * Module#using, which activates refinements of the specified module only in the current class or module definition. + * Module#singleton_class? returns true if the receiver is a singleton class + or false if it is an ordinary class or module. * extended methods: * Module#refine is no longer experimental. @@ -2385,6 +2385,14 @@ rb_mod_cvar_defined(VALUE obj, VALUE iv) return rb_cvar_defined(obj, id); } +static VALUE +rb_mod_singleton_p(VALUE klass) +{ + if (RB_TYPE_P(klass, T_CLASS) && FL_TEST(klass, FL_SINGLETON)) + return Qtrue; + return Qfalse; +} + static struct conv_method_tbl { const char *method; ID id; @@ -3225,6 +3233,7 @@ Init_Object(void) rb_define_method(rb_cModule, "class_variable_defined?", rb_mod_cvar_defined, 1); rb_define_method(rb_cModule, "public_constant", rb_mod_public_constant, -1); /* in variable.c */ rb_define_method(rb_cModule, "private_constant", rb_mod_private_constant, -1); /* in variable.c */ + rb_define_method(rb_cModule, "singleton_class?", rb_mod_singleton_p, 0); rb_define_method(rb_cClass, "allocate", rb_obj_alloc, 0); rb_define_method(rb_cClass, "new", rb_class_new_instance, -1); diff --git a/test/ruby/test_class.rb b/test/ruby/test_class.rb index 7aed078654..47848f3184 100644 --- a/test/ruby/test_class.rb +++ b/test/ruby/test_class.rb @@ -346,4 +346,10 @@ class TestClass < Test::Unit::TestCase assert_empty(added.grep(->(k) {c == k[0]}), bug5283) assert_equal(:foo, d.foo) end + + def test_singleton_class_p + feature7609 = '[ruby-core:51087] [Feature #7609]' + assert_predicate(self.singleton_class, :singleton_class?, feature7609) + assert_not_predicate(self.class, :singleton_class?, feature7609) + end end |