From 770ddcdfe069f8654a07e0fcc758aea591cdd17d Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 24 Apr 2012 03:46:55 +0000 Subject: * 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 --- lib/ostruct.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib') diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 21bda73c37..167b682fa3 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -100,6 +100,19 @@ class OpenStruct @table.each_key{|key| new_ostruct_member(key)} 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 -- cgit v1.2.3