summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--ext/-test-/class/class2name.c14
-rw-r--r--ext/-test-/class/extconf.rb7
-rw-r--r--ext/-test-/class/init.c11
-rw-r--r--test/-ext-/class/test_class2name.rb18
-rw-r--r--variable.c2
6 files changed, 56 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 7bbcc77478..79afd92454 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Oct 20 05:24:29 2013 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * variable.c (rb_class2name): should return real class name, not
+ singleton class or iclass.
+
Sun Oct 20 04:18:48 2013 Aman Gupta <ruby@tmm1.net>
* variable.c (rb_class2name): call rb_tmp_class_path() directly to
diff --git a/ext/-test-/class/class2name.c b/ext/-test-/class/class2name.c
new file mode 100644
index 0000000000..c48df6fb2a
--- /dev/null
+++ b/ext/-test-/class/class2name.c
@@ -0,0 +1,14 @@
+#include <ruby/ruby.h>
+
+static VALUE
+class2name(VALUE self, VALUE klass)
+{
+ const char *name = rb_class2name(klass);
+ return name ? rb_str_new_cstr(name) : Qnil;
+}
+
+void
+Init_class2name(VALUE klass)
+{
+ rb_define_singleton_method(klass, "class2name", class2name, 1);
+}
diff --git a/ext/-test-/class/extconf.rb b/ext/-test-/class/extconf.rb
new file mode 100644
index 0000000000..a07d660b87
--- /dev/null
+++ b/ext/-test-/class/extconf.rb
@@ -0,0 +1,7 @@
+$INCFLAGS << " -I$(topdir) -I$(top_srcdir)"
+$srcs = Dir[File.join($srcdir, "*.{#{SRC_EXT.join(%q{,})}}")]
+inits = $srcs.map {|s| File.basename(s, ".*")}
+inits.delete("init")
+inits.map! {|s|"X(#{s})"}
+$defs << "-DTEST_INIT_FUNCS(X)=\"#{inits.join(' ')}\""
+create_makefile("-test-/class")
diff --git a/ext/-test-/class/init.c b/ext/-test-/class/init.c
new file mode 100644
index 0000000000..ed715c1942
--- /dev/null
+++ b/ext/-test-/class/init.c
@@ -0,0 +1,11 @@
+#include "ruby.h"
+
+#define init(n) {void Init_##n(VALUE mod); Init_##n(mod);}
+
+void
+Init_class(void)
+{
+ VALUE mBug = rb_define_module("Bug");
+ VALUE mod = rb_define_module_under(mBug, "Class");
+ TEST_INIT_FUNCS(init);
+}
diff --git a/test/-ext-/class/test_class2name.rb b/test/-ext-/class/test_class2name.rb
new file mode 100644
index 0000000000..070be5a130
--- /dev/null
+++ b/test/-ext-/class/test_class2name.rb
@@ -0,0 +1,18 @@
+require 'test/unit'
+require "-test-/class"
+
+class Test_Class < Test::Unit::TestCase
+ class Test_Class2Name < superclass
+ def test_toplevel_class
+ assert_equal("Object", Bug::Class.class2name(::Object))
+ end
+
+ def test_toplevel_module
+ assert_equal("Kernel", Bug::Class.class2name(::Kernel))
+ end
+
+ def test_singleton_class
+ assert_equal("Object", Bug::Class.class2name(::Object.new.singleton_class))
+ end
+ end
+end
diff --git a/variable.c b/variable.c
index 1614afb742..9da4ee217d 100644
--- a/variable.c
+++ b/variable.c
@@ -386,7 +386,7 @@ const char *
rb_class2name(VALUE klass)
{
int permanent;
- VALUE path = rb_tmp_class_path(klass, &permanent, rb_ivar_set);
+ VALUE path = rb_tmp_class_path(rb_class_real(klass), &permanent, rb_ivar_set);
if (NIL_P(path)) return NULL;
return RSTRING_PTR(path);
}