summaryrefslogtreecommitdiff
path: root/lib/ostruct.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-04-24 03:46:55 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-04-24 03:46:55 +0000
commit770ddcdfe069f8654a07e0fcc758aea591cdd17d (patch)
tree36c7009eeab34ae5c08ac62d6005c9ee145b46a3 /lib/ostruct.rb
parentb05c95d93f39b95c6c743282ce4d9e674453f12f (diff)
* hash.c, object.c, struct.c, lib/ostruct.rb: add to_h methods.
[Feature #6276] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35452 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/ostruct.rb')
-rw-r--r--lib/ostruct.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 21bda73..167b682 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -101,6 +101,19 @@ class OpenStruct
end
#
+ # Converts the OpenStruct to a hash with keys representing
+ # each attribute (as symbols) and their corresponding values
+ # Example:
+ #
+ # require 'ostruct'
+ # data = OpenStruct.new("country" => "Australia", :population => 20_000_000)
+ # data.to_h # => {:country => "Australia", :population => 20000000 }
+ #
+ def to_h
+ @table.dup
+ 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.