[ruby/ostruct] Allow overriding public methods
[Fixes https://bugs.ruby-lang.org/issues/15409]
This commit is contained in:
parent
ebb8de7302
commit
8eefa8f373
@ -100,9 +100,9 @@ class OpenStruct
|
|||||||
|
|
||||||
# Duplicates an OpenStruct object's Hash table.
|
# Duplicates an OpenStruct object's Hash table.
|
||||||
def initialize_copy(orig) # :nodoc:
|
def initialize_copy(orig) # :nodoc:
|
||||||
|
orig.table.each_key{|key| new_ostruct_member!(key)}
|
||||||
super
|
super
|
||||||
@table = @table.dup
|
@table = @table.dup
|
||||||
@table.each_key{|key| new_ostruct_member!(key)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -159,8 +159,8 @@ class OpenStruct
|
|||||||
# Provides marshalling support for use by the Marshal library.
|
# Provides marshalling support for use by the Marshal library.
|
||||||
#
|
#
|
||||||
def marshal_load(x)
|
def marshal_load(x)
|
||||||
|
x.each_key{|key| new_ostruct_member!(key)}
|
||||||
@table = x
|
@table = x
|
||||||
@table.each_key{|key| new_ostruct_member!(key)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -169,7 +169,7 @@ class OpenStruct
|
|||||||
# define_singleton_method for both the getter method and the setter method.
|
# define_singleton_method for both the getter method and the setter method.
|
||||||
#
|
#
|
||||||
def new_ostruct_member!(name) # :nodoc:
|
def new_ostruct_member!(name) # :nodoc:
|
||||||
unless respond_to?(name)
|
unless @table.key?(name)
|
||||||
define_singleton_method(name) { @table[name] }
|
define_singleton_method(name) { @table[name] }
|
||||||
define_singleton_method("#{name}=") {|x| @table[name] = x}
|
define_singleton_method("#{name}=") {|x| @table[name] = x}
|
||||||
end
|
end
|
||||||
|
@ -235,4 +235,10 @@ class TC_OpenStruct < Test::Unit::TestCase
|
|||||||
assert_equal(:foo, os.puts)
|
assert_equal(:foo, os.puts)
|
||||||
assert_equal(:bar, os.format)
|
assert_equal(:bar, os.format)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_overriden_public_methods
|
||||||
|
os = OpenStruct.new(method: :foo, class: :bar)
|
||||||
|
assert_equal(:foo, os.method)
|
||||||
|
assert_equal(:bar, os.class)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user