[#18121] [Ruby 1.8.7 - Bug #405] (Open) ssl.rb:31: [BUG] Bus Error — Anonymous <redmine@...>

Issue #405 has been reported by Anonymous.

14 messages 2008/08/04

[#18130] Re: New array methods cycle, choice, shuffle (plus bug in cycle) — Brian Candler <B.Candler@...>

> Seriously though... Array.first is a noun.

10 messages 2008/08/05

[#18319] NEW Command: absolute_path() -- — "C.E. Thornton" <admin@...>

Core,

14 messages 2008/08/16
[#18321] Re: NEW Command: absolute_path() -- — Yukihiro Matsumoto <matz@...> 2008/08/18

Hi,

[#18381] [Bug #496] DRb.start_service(nil) is very slow — Hongli Lai <redmine@...>

Bug #496: DRb.start_service(nil) is very slow

11 messages 2008/08/25

[ruby-core:18426] OpenStruct Patch

From: Trans <transfire@...>
Date: 2008-08-31 16:18:03 UTC
List: ruby-core #18426
Sun Aug 31 20:49:42 2008  Thomas Sawyer  <transfire@gmail.com>

       * lib/ostruct.rb ([]): add [] method.
          Allows members to be accessed by name.

       * lib/ostruct.rb ([]=): add []= method.
          Allows members to be set by name.

       * lib/ostruct.rb (initialize): add support for block initialization.
          OpenStruct can take a self yielding block.

T.

Attachments (1)

ruby-ostruct.patch (1.52 KB, text/x-diff)
Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 19001)
+++ lib/ostruct.rb	(working copy)
@@ -41,6 +41,23 @@
   #
   #   p data        # -> <OpenStruct country="Australia" population=20000000>
   #
+  # Also the OpenStruct can be initialized with a block:
+  #
+  #   data = OpenStruct.new do |p|
+  #     p.name    = 'John'
+  #     p.gender  = :M
+  #     p.age     = 71
+  #   end 
+  #
+  # The two approaches can be combine, where the block take precedence.
+  #
+  #   data = OpenStruct.new(:name => 'John', :age => 31) do |p|
+  #     p.gender = :M 
+  #     p.age = 29
+  #   end
+  #
+  #   p data        # -> <OpenStruct name="John" gender=:M  age="29">
+  #
   # By default, the resulting OpenStruct object will have no attributes. 
   #
   def initialize(hash=nil)
@@ -51,6 +68,7 @@
         new_ostruct_member(k)
       end
     end
+    yield self if block_given?
   end
 
   # Duplicate an OpenStruct object members. 
@@ -77,6 +95,28 @@
     end
   end
 
+  # Access a value in the OpenStruct by name.
+  #
+  #   o = OpenStruct.new
+  #   o.t = 4
+  #   o['t']  #=> 4
+  #
+  def [](name)
+    @table[name.to_sym]
+  end
+
+  # Set a value in the OpenStruct by name.
+  #
+  #   o = OpenStruct.new
+  #   o['t'] = 4
+  #   o.t  #=> 4
+  #
+  def []=(name, val)
+    raise TypeError, "can't modify frozen #{self.class}", caller(1) if frozen?
+    @table[name.to_sym]=val
+  end
+
+
   def method_missing(mid, *args) # :nodoc:
     mname = mid.id2name
     len = args.length

In This Thread

Prev Next