class ActiveSupport::OrderedOptions
h.dog! # => raises KeyError: :dog is blank
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 can be written as:<br><br>h # => nil<br>h # => ‘Mary’<br>h # => ‘John’<br>h = ‘Mary’<br>h = ‘John’
h = {}
With a Hash
, key-value pairs are typically managed like this:OrderedOptions
inherits from Hash
and provides dynamic accessor methods.
def [](key)
def [](key) super(key.to_sym) end
def []=(key, value)
def []=(key, value) super(key.to_sym, value) end
def extractable_options?
def extractable_options? true end
def inspect
def inspect "#<#{self.class.name} #{super}>" 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 self[name_string].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