[前][次][番号順一覧][スレッド一覧]

ruby-changes:25324

From: marcandre <ko1@a...>
Date: Mon, 29 Oct 2012 06:20:23 +0900 (JST)
Subject: [ruby-changes:25324] marcandRe: r37376 (trunk): * lib/ostruct.rb: Add [] and []=, base on a patch by Thomas Sawyer

marcandre	2012-10-29 06:20:10 +0900 (Mon, 29 Oct 2012)

  New Revision: 37376

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=37376

  Log:
    * lib/ostruct.rb: Add [] and []=, base on a patch by Thomas Sawyer
    [ruby-core:42779] [Feature #6056]

  Modified files:
    trunk/NEWS
    trunk/lib/ostruct.rb
    trunk/test/ostruct/test_ostruct.rb

Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 37375)
+++ lib/ostruct.rb	(revision 37376)
@@ -176,19 +176,39 @@
   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.
   #
Index: NEWS
===================================================================
--- NEWS	(revision 37375)
+++ NEWS	(revision 37376)
@@ -118,6 +118,7 @@
 
 * ostruct
   * new methods:
+    * OpenStruct#[], []=
     * OpenStruct#each_pair
     * OpenStruct#eql?
     * OpenStruct#hash
Index: test/ostruct/test_ostruct.rb
===================================================================
--- test/ostruct/test_ostruct.rb	(revision 37375)
+++ test/ostruct/test_ostruct.rb	(revision 37376)
@@ -70,14 +70,19 @@
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]