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

ruby-changes:23291

From: marcandre <ko1@a...>
Date: Mon, 16 Apr 2012 12:16:33 +0900 (JST)
Subject: [ruby-changes:23291] marcandRe: r35342 (trunk): * lib/ostruct.rb: Add OpenStruct#to_h [Feature #6276]

marcandre	2012-04-16 12:16:25 +0900 (Mon, 16 Apr 2012)

  New Revision: 35342

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

  Log:
    * lib/ostruct.rb: Add OpenStruct#to_h [Feature #6276]
      [ref #1400] [rubyspec:9e0250b2fc6f]

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

Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 35341)
+++ lib/ostruct.rb	(revision 35342)
@@ -101,33 +101,28 @@
   end
 
   #
-  # Provides marshalling support for use by the Marshal library. Returning the
-  # underlying Hash table that contains the functions defined as the keys and
-  # the values assigned to them.
+  # Converts the OpenStruct to a hash with keys representing
+  # each attribute (as symbols) and their corresponding values
+  # Example:
   #
-  #    require 'ostruct'
+  #   require 'ostruct'
+  #   data = OpenStruct.new("country" => "Australia", :population => 20_000_000)
+  #   data.to_h   # => {:country => "Australia", :population => 20000000 }
   #
-  #    person = OpenStruct.new
-  #    person.name = 'John Smith'
-  #    person.age  = 70
+  def to_h
+    @table.dup
+  end
+
   #
-  #    person.marshal_dump # => { :name => 'John Smith', :age => 70 }
+  # Provides marshalling support for use by the Marshal library.
   #
   def marshal_dump
     @table
   end
 
   #
-  # Provides marshalling support for use by the Marshal library. Accepting
-  # a Hash of keys and values which will be used to populate the internal table
+  # Provides marshalling support for use by the Marshal library.
   #
-  #    require 'ostruct'
-  #
-  #    event = OpenStruct.new
-  #    hash = { 'time' => Time.now, 'title' => 'Birthday Party' }
-  #    event.marshal_load(hash)
-  #    event.title # => 'Birthday Party'
-  #
   def marshal_load(x)
     @table = x
     @table.each_key{|key| new_ostruct_member(key)}
Index: NEWS
===================================================================
--- NEWS	(revision 35341)
+++ NEWS	(revision 35342)
@@ -74,6 +74,10 @@
     * Net::IMAP.default_ssl_port
     * Net::IMAP.default_imaps_port
 
+* ostruct
+  * new methods:
+    * OpenStruct#to_h converts the struct to a hash.
+
 * pathname
   * extended method:
     * Pathname#find returns an enumerator if no block is given.

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

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