class ActiveSupport::OrderedOptions
h.dog! # => raises KeyError: key not found: :dog
bang to the key name, like:
To raise an exception when the value is blank, append a
h.dog # => nil
h.girl # => ‘Mary’
h.boy # => ‘John’
h.girl = ‘Mary’
h.boy = ‘John’
h = ActiveSupport::OrderedOptions.new
Using OrderedOptions
, the above code could be reduced to:<br><br>h # => nil<br>h # => ‘Mary’<br>h # => ‘John’<br>h = ‘Mary’<br>h = ‘John’
h = {}
Usually key value pairs are handled something like this:
def [](key)
def [](key) super(key.to_sym) end
def []=(key, value)
def []=(key, value) super(key.to_sym, value) end
def method_missing(name, *args)
def method_missing(name, *args) name_string = name.to_s if name_string.chomp!('=') self[name_string] = args.first else bangs = name_string.chomp!('!') if bangs fetch(name_string.to_sym).presence || raise(KeyError.new("#{name_string} is blank.")) else self[name_string] end end end
def respond_to_missing?(name, include_private)
def respond_to_missing?(name, include_private) true end