1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

object.c: Module#singleton_class?

* 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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42449 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-08-08 14:01:23 +00:00
parent 44aa21d4aa
commit a25d02b144
4 changed files with 23 additions and 0 deletions

View file

@ -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.

2
NEWS
View file

@ -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.

View file

@ -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);

View file

@ -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