class Hash

Public Instance Methods

method_missing(s, *args) click to toggle source
Calls superclass method
# File lib/helperclasses/hashaccessor.rb, line 22
def method_missing(s, *args)
  case s.to_s
    when "to_ary"
      super(s, args)
    when /^_.*[^=]$/
      key = s.to_s.sub(/^_{1,2}/, '').to_sym
      self.has_key? key and return self[key]
      self.has_key? key.to_s and return self[key.to_s]
      if s.to_s =~ /^__/
        return self[key] = {}
      else
        return nil
      end
    when /^_.*=$/
      key = /^_{1,2}(.*)=$/.match(s.to_s)[1].to_sym
      self.has_key? key and return self[key] = args[0]
      self.has_key? key.to_s and return self[key.to_s] = args[0]
      return self[key] = args[0]
    else
      super(s, args)
  end
end
to_sym() click to toggle source

Converts all keys of a hash to syms recursively

# File lib/helperclasses/hashaccessor.rb, line 10
def to_sym
  ret = {}
  each { |k, v|
    ret[k.to_sym] = v.class == Hash ? v.to_sym : v
  }
  ret
end
to_sym!() click to toggle source
# File lib/helperclasses/hashaccessor.rb, line 18
def to_sym!
  self.replace(to_sym())
end