1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
marcandre 2015-12-31 05:37:21 +00:00
parent 99894e6c82
commit 2f2a5c3ae9
3 changed files with 23 additions and 1 deletions

View file

@ -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

View file

@ -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

View file

@ -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