Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/smeagol/hash.rb

Instance Method Summary collapse

Instance Method Details

#to_ostructObject

Recursively converts a Hash object to an OpenStruct object.

Returns a copy of the hash as an OpenStruct.



5
6
7
8
9
10
11
12
# File 'lib/smeagol/hash.rb', line 5

def to_ostruct()
  struct = OpenStruct.new(self.clone())
  each do |k, v|
    struct.__send__("#{k}=", v.to_ostruct()) if v.is_a?(Hash)
    struct.__send__(k).map! {|x| x.is_a?(Hash) ? x.to_ostruct() : x} if v.is_a?(Array)
  end
  return struct
end