[前][次][番号順一覧][スレッド一覧]

ruby-changes:28127

From: naruse <ko1@a...>
Date: Mon, 8 Apr 2013 12:06:21 +0900 (JST)
Subject: [ruby-changes:28127] naruse:r40178 (trunk): * object.c (rb_obj_ivar_set): call to_str for string only once.

naruse	2013-04-08 12:05:15 +0900 (Mon, 08 Apr 2013)

  New Revision: 40178

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40178

  Log:
    * object.c (rb_obj_ivar_set): call to_str for string only once.

  Modified files:
    trunk/ChangeLog
    trunk/object.c
    trunk/test/ruby/test_module.rb
    trunk/test/ruby/test_object.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 40177)
+++ ChangeLog	(revision 40178)
@@ -1,13 +1,11 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
-Sun Apr  7 21:27:40 2013  NARUSE, Yui  <naruse@r...>
+Mon Apr  8 12:05:02 2013  NARUSE, Yui  <naruse@r...>
 
-	* object.c (rb_mod_const_set): call to_str for string only once.
+	* object.c (rb_obj_ivar_set): call to_str for string only once.
 	  to_str was called from rb_is_const_name and rb_to_id before.
 
+	* object.c (rb_mod_const_set): ditto.
 
-Sun Apr  7 21:16:19 2013  NARUSE, Yui  <naruse@r...>
-
-	* object.c (rb_mod_cvar_set): call to_str for string only once.
-	  to_str was called from rb_is_class_name and rb_to_id before.
+	* object.c (rb_mod_cvar_set): ditto.
 
 Sun Apr  7 13:56:16 2013  Kazuhiro NISHIYAMA  <zn@m...>
 
Index: object.c
===================================================================
--- object.c	(revision 40177)
+++ object.c	(revision 40178)
@@ -2189,12 +2189,16 @@ rb_obj_ivar_set(VALUE obj, VALUE iv, VAL https://github.com/ruby/ruby/blob/trunk/object.c#L2189
 			  QUOTE_ID(id));
 	}
     }
-    else if (!rb_is_instance_name(iv)) {
+    else {
+	VALUE name = rb_check_string_type(iv);
+       if (NIL_P(name)) {
+           rb_raise(rb_eTypeError, "%+"PRIsVALUE" is not a symbol or string", iv);
+       }
+	if (!rb_is_instance_name(name)) {
 	rb_name_error_str(iv, "`%"PRIsVALUE"' is not allowed as an instance variable name",
 			  QUOTE(iv));
-    }
-    else {
-	id = rb_to_id(iv);
+	}
+	id = rb_to_id(name);
     }
     return rb_ivar_set(obj, id, val);
 }
Index: test/ruby/test_module.rb
===================================================================
--- test/ruby/test_module.rb	(revision 40177)
+++ test/ruby/test_module.rb	(revision 40178)
@@ -262,6 +262,12 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L262
   def test_const_get
     assert_equal(Math::PI, Math.const_get("PI"))
     assert_equal(Math::PI, Math.const_get(:PI))
+
+    n = Object.new
+    def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "PI"; end
+    def n.count; @count; end
+    assert_equal(Math::PI, Math.const_get(n))
+    assert_equal(1, n.count)
   end
 
   def test_nested_get
@@ -302,6 +308,20 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L308
     assert_equal(99, Other::KOALA)
     Other.const_set("WOMBAT", "Hi")
     assert_equal("Hi", Other::WOMBAT)
+
+    n = Object.new
+    def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "HOGE"; end
+    def n.count; @count; end
+    def n.count=(v); @count=v; end
+    assert(!Other.const_defined?(:HOGE))
+    Other.const_set(n, 999)
+    assert_equal(1, n.count)
+    n.count = 0
+    assert_equal(999, Other.const_get(n))
+    assert_equal(1, n.count)
+    n.count = 0
+    assert_equal(true, Other.const_defined?(n))
+    assert_equal(1, n.count)
   end
 
   def test_constants
@@ -669,6 +689,12 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L689
     assert_raise(NameError) { c.class_variable_get(:foo) }
     assert_raise(NameError) { c.class_variable_get("bar") }
     assert_raise(TypeError) { c.class_variable_get(1) }
+
+    n = Object.new
+    def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
+    def n.count; @count; end
+    assert_equal(:foo, c.class_variable_get(n))
+    assert_equal(1, n.count)
   end
 
   def test_class_variable_set
@@ -679,6 +705,13 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L705
     assert_raise(NameError) { c.class_variable_set(:foo, 1) }
     assert_raise(NameError) { c.class_variable_set("bar", 1) }
     assert_raise(TypeError) { c.class_variable_set(1, 1) }
+
+    n = Object.new
+    def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
+    def n.count; @count; end
+    c.class_variable_set(n, :bar)
+    assert_equal(:bar, c.class_eval('@@foo'))
+    assert_equal(1, n.count)
   end
 
   def test_class_variable_defined
@@ -689,6 +722,11 @@ class TestModule < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_module.rb#L722
     assert_raise(NameError) { c.class_variable_defined?(:foo) }
     assert_raise(NameError) { c.class_variable_defined?("bar") }
     assert_raise(TypeError) { c.class_variable_defined?(1) }
+    n = Object.new
+    def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@@foo"; end
+    def n.count; @count; end
+    assert_equal(true, c.class_variable_defined?(n))
+    assert_equal(1, n.count)
   end
 
   def test_remove_class_variable
Index: test/ruby/test_object.rb
===================================================================
--- test/ruby/test_object.rb	(revision 40177)
+++ test/ruby/test_object.rb	(revision 40178)
@@ -176,6 +176,12 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L176
     assert_raise(NameError) { o.instance_variable_get(:foo) }
     assert_raise(NameError) { o.instance_variable_get("bar") }
     assert_raise(TypeError) { o.instance_variable_get(1) }
+
+    n = Object.new
+    def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
+    def n.count; @count; end
+    assert_equal(:foo, o.instance_variable_get(n))
+    assert_equal(1, n.count)
   end
 
   def test_instance_variable_set
@@ -185,6 +191,13 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L191
     assert_raise(NameError) { o.instance_variable_set(:foo, 1) }
     assert_raise(NameError) { o.instance_variable_set("bar", 1) }
     assert_raise(TypeError) { o.instance_variable_set(1, 1) }
+
+    n = Object.new
+    def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
+    def n.count; @count; end
+    o.instance_variable_set(n, :bar)
+    assert_equal(:bar, o.instance_eval { @foo })
+    assert_equal(1, n.count)
   end
 
   def test_instance_variable_defined
@@ -195,6 +208,12 @@ class TestObject < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_object.rb#L208
     assert_raise(NameError) { o.instance_variable_defined?(:foo) }
     assert_raise(NameError) { o.instance_variable_defined?("bar") }
     assert_raise(TypeError) { o.instance_variable_defined?(1) }
+
+    n = Object.new
+    def n.to_str; @count = defined?(@count) ? @count + 1 : 1; "@foo"; end
+    def n.count; @count; end
+    assert_equal(true, o.instance_variable_defined?(n))
+    assert_equal(1, n.count)
   end
 
   def test_remove_instance_variable

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]