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

ruby-changes:41323

From: marcandre <ko1@a...>
Date: Thu, 31 Dec 2015 14:37:28 +0900 (JST)
Subject: [ruby-changes:41323] marcandRe: r53395 (trunk): * lib/ostruct.rb: Fix new_ostruct_member to correctly avoid redefinition

marcandre	2015-12-31 14:37:21 +0900 (Thu, 31 Dec 2015)

  New Revision: 53395

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

  Log:
    * lib/ostruct.rb: Fix new_ostruct_member to correctly avoid redefinition
      [#11901]

  Modified files:
    trunk/ChangeLog
    trunk/lib/ostruct.rb
    trunk/test/ostruct/test_ostruct.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53394)
+++ ChangeLog	(revision 53395)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Dec 31 14:36:45 2015  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/ostruct.rb: Fix new_ostruct_member to correctly avoid
+	  redefinition [#11901]
+
 Thu Dec 31 02:45:12 2015  NARUSE, Yui  <naruse@r...>
 
 	* test/ruby/test_module.rb (test_classpath): r53376 may change
Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 53394)
+++ lib/ostruct.rb	(revision 53395)
@@ -168,7 +168,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L168
   #
   def new_ostruct_member(name)
     name = name.to_sym
-    unless respond_to?(name)
+    unless singleton_class.method_defined?(name)
       define_singleton_method(name) { @table[name] }
       define_singleton_method("#{name}=") { |x| modifiable[name] = x }
     end
Index: test/ostruct/test_ostruct.rb
===================================================================
--- test/ostruct/test_ostruct.rb	(revision 53394)
+++ test/ostruct/test_ostruct.rb	(revision 53395)
@@ -164,4 +164,21 @@ class TC_OpenStruct < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L164
     e = assert_raise(ArgumentError) { os.send :foo=, true, true }
     assert_match(/#{__callee__}/, e.backtrace[0])
   end
+
+  def test_accessor_defines_method
+    os = OpenStruct.new(foo: 42)
+    assert os.respond_to? :foo
+    assert_equal([], os.singleton_methods)
+    assert_equal(42, os.foo)
+    assert_equal([:foo, :foo=], os.singleton_methods)
+  end
+
+  def test_does_not_redefine
+    os = OpenStruct.new(foo: 42)
+    def os.foo
+      43
+    end
+    os.foo = 44
+    assert_equal(43, os.foo)
+  end
 end

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

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