summaryrefslogtreecommitdiff
path: root/lib/ostruct.rb
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-04-18 03:59:15 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-04-18 03:59:15 +0000
commit62c9ecfa122da546af36ed5a31e8696c62ced036 (patch)
tree40fd17671b67821d024a1d47beb5a22d1faad43b /lib/ostruct.rb
parentcc6ab53f592016daf6f4ef0075e4f33f365d638f (diff)
Revert r35339-35343 because of no tests.
* hash.c: Alias ENV.to_h to ENV.to_hash [ref #6276] * lib/ostruct.rb: Add OpenStruct#to_h [Feature #6276] * struct.c: Add Struct#to_h [Feature #6276] * object.c: Add NilClass#to_h [Feature #6276] * hash.c: Add Hash#to_h [Feature #6276] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35383 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/ostruct.rb')
-rw-r--r--lib/ostruct.rb29
1 files changed, 17 insertions, 12 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index da0cf0b..21bda73 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -101,27 +101,32 @@ class OpenStruct
end
#
- # Converts the OpenStruct to a hash with keys representing
- # each attribute (as symbols) and their corresponding values
- # Example:
+ # 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.
#
- # require 'ostruct'
- # data = OpenStruct.new("country" => "Australia", :population => 20_000_000)
- # data.to_h # => {:country => "Australia", :population => 20000000 }
+ # require 'ostruct'
#
- def to_h
- @table.dup
- end
-
+ # person = OpenStruct.new
+ # person.name = 'John Smith'
+ # person.age = 70
#
- # Provides marshalling support for use by the Marshal library.
+ # person.marshal_dump # => { :name => 'John Smith', :age => 70 }
#
def marshal_dump
@table
end
#
- # Provides marshalling support for use by the Marshal library.
+ # 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
+ #
+ # 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