mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
merge revision(s) 53395,53396: [Backport #11901]
* lib/ostruct.rb: Fix new_ostruct_member to correctly avoid redefinition [#11901] * lib/ostruct.rb (freeze): define deferred accessors before freezing to get rid of an error when just reading frozen OpenStruct. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_3@54388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
4c1ac0bc07
commit
4f85a89a9f
4 changed files with 35 additions and 2 deletions
11
ChangeLog
11
ChangeLog
|
@ -1,3 +1,14 @@
|
|||
Tue Mar 29 18:49:54 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* lib/ostruct.rb (freeze): define deferred accessors before
|
||||
freezing to get rid of an error when just reading frozen
|
||||
OpenStruct.
|
||||
|
||||
Tue Mar 29 18:49:54 2016 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||
|
||||
* lib/ostruct.rb: Fix new_ostruct_member to correctly avoid
|
||||
redefinition [#11901]
|
||||
|
||||
Tue Mar 29 17:54:17 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* lib/ostruct.rb (OpenStruct): make respond_to? working on
|
||||
|
|
|
@ -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
|
||||
|
@ -176,6 +176,11 @@ class OpenStruct
|
|||
end
|
||||
protected :new_ostruct_member
|
||||
|
||||
def freeze
|
||||
@table.each_key {|key| new_ostruct_member(key)}
|
||||
super
|
||||
end
|
||||
|
||||
def respond_to_missing?(mid, include_private = false)
|
||||
mname = mid.to_s.chomp("=").to_sym
|
||||
@table.key?(mname) || super
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#define RUBY_VERSION "2.3.0"
|
||||
#define RUBY_RELEASE_DATE "2016-03-29"
|
||||
#define RUBY_PATCHLEVEL 44
|
||||
#define RUBY_PATCHLEVEL 45
|
||||
|
||||
#define RUBY_RELEASE_YEAR 2016
|
||||
#define RUBY_RELEASE_MONTH 3
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue