[#28015] RCR: RUBY_VERSION_INT — Roger Pack <rogerdpack2@...>

Situation:

14 messages 2010/02/02

[#28113] [Bug #2723] $: length affects re-require time of already loaded files — Greg Hazel <redmine@...>

Bug #2723: $: length affects re-require time of already loaded files

16 messages 2010/02/08

[#28151] [Bug #2739] ruby 1.8.7 built with pthreads hangs under some circumstances — Joel Ebel <redmine@...>

Bug #2739: ruby 1.8.7 built with pthreads hangs under some circumstances

31 messages 2010/02/11

[#28188] [Bug #2750] build fails on win32/MinGW: "executable host ruby is required." even when --with-baseruby is used — Christian Bodt <redmine@...>

Bug #2750: build fails on win32/MinGW: "executable host ruby is required." even when --with-baseruby is used

9 messages 2010/02/16

[#28206] Is Math module a wrapper of libm? — Yusuke ENDOH <mame@...>

Hi matz --

23 messages 2010/02/18
[#28212] Re: Is Math module a wrapper of libm? — Yukihiro Matsumoto <matz@...> 2010/02/18

Hi,

[#28219] Re: Is Math module a wrapper of libm? — Yusuke ENDOH <mame@...> 2010/02/18

Hi,

[#28225] Re: Is Math module a wrapper of libm? — Marc-Andre Lafortune <ruby-core-mailing-list@...> 2010/02/18

Hi,

[#28233] Re: Is Math module a wrapper of libm? — Kenta Murata <muraken@...> 2010/02/18

Hi,

[#28265] Re: Is Math module a wrapper of libm? — Marc-Andre Lafortune <ruby-core-mailing-list@...> 2010/02/20

Hi,

[#28286] Re: Is Math module a wrapper of libm? — Kenta Murata <muraken@...> 2010/02/21

Hi

[#28291] Re: Is Math module a wrapper of libm? — Marc-Andre Lafortune <ruby-core-mailing-list@...> 2010/02/22

Hi!

[#28235] [Feature #2759] Regexp /g and /G options — Michael Fellinger <redmine@...>

Feature #2759: Regexp /g and /G options

35 messages 2010/02/18
[#28459] [Feature #2759] Regexp /g and /G options — caleb clausen <redmine@...> 2010/03/04

Issue #2759 has been updated by caleb clausen.

[#28329] [ANN] Ruby 1.9.2dev has passed RubySpec! — Yusuke ENDOH <mame@...>

Hi,

12 messages 2010/02/24

[#28355] [ANN] Toward rich diversity of Ruby development. — Urabe Shyouhei <shyouhei@...>

A short announcement: thanks to some helps of GitHub people, I now have

12 messages 2010/02/27

[#28365] Indentifying key MRI-on-Windows issues — Jon <jon.forums@...>

In an effort to begin summarizing key MRI-on-Windows open issues I'm starting this thread in hopes that those interested will respond with details on the key MRI issues they feel need resolution for Windows users.

11 messages 2010/02/27
[#28690] Re: Indentifying key MRI-on-Windows issues — Roger Pack <rogerdpack2@...> 2010/03/16

> My key concern is http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/24968

[ruby-core:28338] Re: [Feature #1082] add Object#singleton_class method

From: Shugo Maeda <shugo@...>
Date: 2010-02-25 15:07:49 UTC
List: ruby-core #28338
Hi,

2010/2/23 Shugo Maeda <shugo@ruby-lang.org>:
> Then, how about to introduce singleton_class as just a short form of
> class <<obj; self; end at first?

I have attached a patch with a test.

-- 
Shugo Maeda

Attachments (1)

singleton_class.diff (2.09 KB, text/x-diff)
diff --git a/object.c b/object.c
index 0421824..0b895b5 100644
--- a/object.c
+++ b/object.c
@@ -162,6 +162,29 @@ rb_obj_class(VALUE obj)
     return rb_class_real(CLASS_OF(obj));
 }
 
+/*
+ *  call-seq:
+ *     obj.singleton_class    => class
+ *
+ *  Returns the singleton class of <i>obj</i>.  This method creates
+ *  a new singleton class if <i>obj</i> does not have it.
+ *
+ *  If <i>obj</i> is <code>nil</code>, <code>true</code>, or
+ *  <code>false</code>, it returns NilClass, TrueClass, or FalseClass,
+ *  respectively.
+ *  If <i>obj</i> is a Fixnum or a Symbol, it raises a TypeError.
+ *
+ *     Object.new.singleton_class  #=> #<Class:#<Object:0xb7ce1e24>>
+ *     String.singleton_class      #=> #<Class:String>
+ *     nil.singleton_class         #=> NilClass
+ */
+
+static VALUE
+rb_obj_singleton_class(VALUE obj)
+{
+    return rb_singleton_class(obj);
+}
+
 static void
 init_copy(VALUE dest, VALUE obj)
 {
@@ -2585,6 +2608,7 @@ Init_Object(void)
     rb_define_method(rb_mKernel, "<=>", rb_obj_cmp, 1);
 
     rb_define_method(rb_mKernel, "class", rb_obj_class, 0);
+    rb_define_method(rb_mKernel, "singleton_class", rb_obj_singleton_class, 0);
     rb_define_method(rb_mKernel, "clone", rb_obj_clone, 0);
     rb_define_method(rb_mKernel, "dup", rb_obj_dup, 0);
     rb_define_method(rb_mKernel, "initialize_copy", rb_obj_init_copy, 1);
diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb
index 0a49422..63c1d32 100644
--- a/test/ruby/test_object.rb
+++ b/test/ruby/test_object.rb
@@ -561,4 +561,25 @@ class TestObject < Test::Unit::TestCase
       end
     end.call
   end
+
+  def test_singleton_class
+    x = Object.new
+    xs = class << x; self; end
+    assert_equal(xs, x.singleton_class)
+
+    y = Object.new
+    ys = y.singleton_class
+    assert_equal(class << y; self; end, ys)
+
+    assert_equal(NilClass, nil.singleton_class)
+    assert_equal(TrueClass, true.singleton_class)
+    assert_equal(FalseClass, false.singleton_class)
+
+    assert_raise(TypeError) do
+      123.singleton_class
+    end
+    assert_raise(TypeError) do
+      :foo.singleton_class
+    end
+  end
 end

In This Thread