summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-31 05:37:21 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-31 05:37:21 +0000
commit2f2a5c3ae9f9e7a4d09b1500de8d864f48d69cad (patch)
tree99f8057a25387d3cf7ec58a6fd53cb41bda03b2b
parent99894e6c82054c893e6c9f5bccc181b97b8120ee (diff)
* lib/ostruct.rb: Fix new_ostruct_member to correctly avoid redefinition
[#11901] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--lib/ostruct.rb2
-rw-r--r--test/ostruct/test_ostruct.rb17
3 files changed, 23 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index a44ab6c6e4..28621c3d18 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Dec 31 14:36:45 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * lib/ostruct.rb: Fix new_ostruct_member to correctly avoid
+ redefinition [#11901]
+
Thu Dec 31 02:45:12 2015 NARUSE, Yui <naruse@ruby-lang.org>
* test/ruby/test_module.rb (test_classpath): r53376 may change
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index e7b8ed4609..e5b69608ad 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -168,7 +168,7 @@ class OpenStruct
#
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
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 8b0424d2d8..02c4c74152 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -164,4 +164,21 @@ class TC_OpenStruct < Test::Unit::TestCase
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