From e44e356b53828d6468114b30e5c169296896f9b1 Mon Sep 17 00:00:00 2001 From: marcandre Date: Sun, 28 Oct 2012 21:20:10 +0000 Subject: * lib/ostruct.rb: Add [] and []=, base on a patch by Thomas Sawyer [ruby-core:42779] [Feature #6056] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@37376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- NEWS | 1 + lib/ostruct.rb | 24 ++++++++++++++++++++++-- test/ostruct/test_ostruct.rb | 17 +++++++++++------ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 3c2040fee4..61b0af5e02 100644 --- a/NEWS +++ b/NEWS @@ -118,6 +118,7 @@ with all sufficient information, see the ChangeLog file. * ostruct * new methods: + * OpenStruct#[], []= * OpenStruct#each_pair * OpenStruct#eql? * OpenStruct#hash diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 17b3ab05e2..b291f08308 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -176,18 +176,38 @@ class OpenStruct def method_missing(mid, *args) # :nodoc: mname = mid.id2name len = args.length - if mname.chomp!('=') && mid != :[]= + if mname.chomp!('=') if len != 1 raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1) end modifiable[new_ostruct_member(mname)] = args[0] - elsif len == 0 && mid != :[] + elsif len == 0 @table[mid] else raise NoMethodError, "undefined method `#{mid}' for #{self}", caller(1) end end + # Returns the value of a member. + # + # person = OpenStruct.new('name' => 'John Smith', 'age' => 70) + # person[:age] # => 70, same as ostruct.age + # + def [](name) + @table[name.to_sym] + end + + # + # Sets the value of a member. + # + # person = OpenStruct.new('name' => 'John Smith', 'age' => 70) + # person[:age] = 42 # => equivalent to ostruct.age = 42 + # person.age # => 42 + # + def []=(name, value) + modifiable[new_ostruct_member(name)] = value + end + # # Remove the named field from the object. Returns the value that the field # contained if it was defined. diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index 491f950838..d82bab9784 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -70,14 +70,19 @@ class TC_OpenStruct < Test::Unit::TestCase assert_equal(a, 'a') end - def test_method_missing_handles_square_bracket_equals - o = OpenStruct.new - assert_raise(NoMethodError) { o[:foo] = :bar } + def test_setter + os = OpenStruct.new + os[:foo] = :bar + assert_equal :bar, os.foo + os['foo'] = :baz + assert_equal :baz, os.foo end - def test_method_missing_handles_square_brackets - o = OpenStruct.new - assert_raise(NoMethodError) { o[:foo] } + def test_getter + os = OpenStruct.new + os.foo = :bar + assert_equal :bar, os[:foo] + assert_equal :bar, os['foo'] end def test_to_h -- cgit v1.2.3