Class: ActiveSupport::OrderedOptions

Inherits:
Hash show all
Defined in:
lib/active_support/ordered_options.rb

Overview

Usually key value pairs are handled something like this:

h = {}
h[:boy] = 'John'
h[:girl] = 'Mary'
h[:boy]  # => 'John'
h[:girl] # => 'Mary'

Using OrderedOptions, the above code could be reduced to:

h = ActiveSupport::OrderedOptions.new
h.boy = 'John'
h.girl = 'Mary'
h.boy  # => 'John'
h.girl # => 'Mary'

Direct Known Subclasses

InheritableOptions

Instance Method Summary collapse

Methods inherited from Hash

#as_json, #assert_valid_keys, #deep_dup, #deep_merge, #deep_merge!, #deep_stringify_keys, #deep_stringify_keys!, #deep_symbolize_keys, #deep_symbolize_keys!, #deep_transform_keys, #deep_transform_keys!, #diff, #encode_json, #except, #except!, #extract!, #extractable_options?, from_trusted_xml, from_xml, #reverse_merge, #reverse_merge!, #slice, #slice!, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #to_param, #to_xml, #transform_keys, #transform_keys!, #with_indifferent_access

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/active_support/ordered_options.rb', line 29

def method_missing(name, *args)
  name_string = name.to_s
  if name_string.chomp!('=')
    self[name_string] = args.first
  else
    self[name]
  end
end

Instance Method Details

#[](key) ⇒ Object



25
26
27
# File 'lib/active_support/ordered_options.rb', line 25

def [](key)
  super(key.to_sym)
end

#[]=(key, value) ⇒ Object



21
22
23
# File 'lib/active_support/ordered_options.rb', line 21

def []=(key, value)
  super(key.to_sym, value)
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/active_support/ordered_options.rb', line 38

def respond_to_missing?(name, include_private)
  true
end