From 3cd7b10e2344f89a1dac463de49b0d2b202a5d62 Mon Sep 17 00:00:00 2001 From: crueber Date: Thu, 19 Jan 2012 17:36:44 -0600 Subject: [PATCH] Adding to_hash method in order to enable the use of standard library Hash methods on nested OpenStruct's. --- lib/ostruct.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 3779b2dc00057e..44f7411c0bdbb2 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -235,6 +235,23 @@ def inspect attr_reader :table # :nodoc: protected :table + + # + # Returns a nested hash of all open structs. This enables all hash methods to be easily accessed. + # + def to_hash + outputHash = Hash.new + + @table.each do | k, v | + if v.is_a? OpenStruct + outputHash[k] = v.to_hash + else + outputHash[k] = v + end + end + + outputHash + end # # Compares this object and +other+ for equality. An OpenStruct is equal to