diff options
-rw-r--r-- | lib/ostruct.rb | 6 | ||||
-rw-r--r-- | test/ostruct/test_ostruct.rb | 6 |
2 files changed, 9 insertions, 3 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 18850bfafb..5f3d301ce0 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -100,9 +100,9 @@ class OpenStruct # Duplicates an OpenStruct object's Hash table. def initialize_copy(orig) # :nodoc: + orig.table.each_key{|key| new_ostruct_member!(key)} super @table = @table.dup - @table.each_key{|key| new_ostruct_member!(key)} end # @@ -159,8 +159,8 @@ class OpenStruct # Provides marshalling support for use by the Marshal library. # def marshal_load(x) + x.each_key{|key| new_ostruct_member!(key)} @table = x - @table.each_key{|key| new_ostruct_member!(key)} end # @@ -169,7 +169,7 @@ class OpenStruct # define_singleton_method for both the getter method and the setter method. # def new_ostruct_member!(name) # :nodoc: - unless respond_to?(name) + unless @table.key?(name) define_singleton_method(name) { @table[name] } define_singleton_method("#{name}=") {|x| @table[name] = x} end diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index 0688d8950c..3c934ccbec 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -235,4 +235,10 @@ class TC_OpenStruct < Test::Unit::TestCase assert_equal(:foo, os.puts) assert_equal(:bar, os.format) 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 |