* 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
This commit is contained in:
parent
3785d2675a
commit
e44e356b53
1
NEWS
1
NEWS
@ -118,6 +118,7 @@ with all sufficient information, see the ChangeLog file.
|
|||||||
|
|
||||||
* ostruct
|
* ostruct
|
||||||
* new methods:
|
* new methods:
|
||||||
|
* OpenStruct#[], []=
|
||||||
* OpenStruct#each_pair
|
* OpenStruct#each_pair
|
||||||
* OpenStruct#eql?
|
* OpenStruct#eql?
|
||||||
* OpenStruct#hash
|
* OpenStruct#hash
|
||||||
|
@ -176,18 +176,38 @@ class OpenStruct
|
|||||||
def method_missing(mid, *args) # :nodoc:
|
def method_missing(mid, *args) # :nodoc:
|
||||||
mname = mid.id2name
|
mname = mid.id2name
|
||||||
len = args.length
|
len = args.length
|
||||||
if mname.chomp!('=') && mid != :[]=
|
if mname.chomp!('=')
|
||||||
if len != 1
|
if len != 1
|
||||||
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
|
||||||
end
|
end
|
||||||
modifiable[new_ostruct_member(mname)] = args[0]
|
modifiable[new_ostruct_member(mname)] = args[0]
|
||||||
elsif len == 0 && mid != :[]
|
elsif len == 0
|
||||||
@table[mid]
|
@table[mid]
|
||||||
else
|
else
|
||||||
raise NoMethodError, "undefined method `#{mid}' for #{self}", caller(1)
|
raise NoMethodError, "undefined method `#{mid}' for #{self}", caller(1)
|
||||||
end
|
end
|
||||||
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
|
# Remove the named field from the object. Returns the value that the field
|
||||||
# contained if it was defined.
|
# contained if it was defined.
|
||||||
|
@ -70,14 +70,19 @@ class TC_OpenStruct < Test::Unit::TestCase
|
|||||||
assert_equal(a, 'a')
|
assert_equal(a, 'a')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_method_missing_handles_square_bracket_equals
|
def test_setter
|
||||||
o = OpenStruct.new
|
os = OpenStruct.new
|
||||||
assert_raise(NoMethodError) { o[:foo] = :bar }
|
os[:foo] = :bar
|
||||||
|
assert_equal :bar, os.foo
|
||||||
|
os['foo'] = :baz
|
||||||
|
assert_equal :baz, os.foo
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_method_missing_handles_square_brackets
|
def test_getter
|
||||||
o = OpenStruct.new
|
os = OpenStruct.new
|
||||||
assert_raise(NoMethodError) { o[:foo] }
|
os.foo = :bar
|
||||||
|
assert_equal :bar, os[:foo]
|
||||||
|
assert_equal :bar, os['foo']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_to_h
|
def test_to_h
|
||||||
|
Loading…
x
Reference in New Issue
Block a user