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