Class: Angelo::SymHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/angelo/params_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(h = nil) ⇒ SymHash

Returns a Hash that allows values to be fetched with String or Symbol keys.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/angelo/params_parser.rb', line 70

def initialize h = nil
  super(){|hash,key| hash[key.to_s] if Symbol === key}
  unless h.nil?
    merge! h

    # Replace values that are Hashes with SymHashes, recursively.
    each do |k,v|
      self[k] = case v
                when Hash
                  SymHash.new(v)
                when Array
                  v.map {|e| Hash === e ? SymHash.new(e) : e}
                else
                  v
                end
    end

  end
end