Class: Mash
Overview
Constant Summary
Constants included from ActiveSupport::CoreExtensions::Hash::Conversions
ActiveSupport::CoreExtensions::Hash::Conversions::XML_FORMATTING, ActiveSupport::CoreExtensions::Hash::Conversions::XML_PARSING, ActiveSupport::CoreExtensions::Hash::Conversions::XML_TYPE_NAMES
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #default(key = nil) ⇒ Object
- #delete(key) ⇒ Object
-
#except(*keys) ⇒ Mash
A new mash without the selected keys.
-
#fetch(key, *extras) ⇒ Object
The value at key or the default value.
-
#initialize(constructor = {}) ⇒ Mash
constructor
A new instance of Mash.
-
#key?(key) ⇒ TrueClass, FalseClass
(also: #include?, #has_key?, #member?)
True if the key exists in the mash.
-
#merge(hash) ⇒ Mash
A new mash with the hash values merged in.
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
-
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
-
#to_hash ⇒ Hash
The mash as a Hash with string keys.
-
#update(other_hash) ⇒ Mash
(also: #merge!)
The updated mash.
-
#values_at(*indices) ⇒ Array
The values at each of the provided keys.
Methods inherited from Hash
#-, #add_html_class!, #environmentize_keys!, from_xml, #join, #normalize_param, #only, #protect_keys!, #to_json, #to_mash, #to_params, #to_xml_attributes, #unprotect_keys!
Methods included from ActiveSupport::CoreExtensions::Hash::Except
Methods included from ActiveSupport::CoreExtensions::Hash::Slice
Methods included from ActiveSupport::CoreExtensions::Hash::Diff
Methods included from ActiveSupport::CoreExtensions::Hash::Conversions
Methods included from ActiveSupport::CoreExtensions::Hash::ReverseMerge
#reverse_merge, #reverse_merge!
Methods included from ActiveSupport::CoreExtensions::Hash::DeepMerge
Methods included from ActiveSupport::CoreExtensions::Hash::IndifferentAccess
Methods included from ActiveSupport::CoreExtensions::Hash::Keys
#assert_valid_keys, #stringify_keys, #symbolize_keys, #symbolize_keys!
Constructor Details
Instance Method Details
#[]=(key, value) ⇒ Object
42 43 44 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 42 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#default(key = nil) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 25 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end |
#delete(key) ⇒ Object
95 96 97 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 95 def delete(key) super(convert_key(key)) end |
#except(*keys) ⇒ Mash
Returns A new mash without the selected keys.
106 107 108 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 106 def except(*keys) super(*keys.map {|k| convert_key(k)}) end |
#fetch(key, *extras) ⇒ Object
Returns The value at key or the default value.
74 75 76 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 74 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ TrueClass, FalseClass Also known as: include?, has_key?, member?
Returns True if the key exists in the mash.
61 62 63 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 61 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Mash
Returns A new mash with the hash values merged in.
89 90 91 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 89 def merge(hash) self.dup.update(hash) end |
#regular_update ⇒ Object
34 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 34 alias_method :regular_update, :update |
#regular_writer ⇒ Object
33 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 33 alias_method :regular_writer, :[]= |
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
113 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 113 def stringify_keys!; self end |
#to_hash ⇒ Hash
Returns The mash as a Hash with string keys.
116 117 118 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 116 def to_hash Hash.new(default).merge(self) end |
#update(other_hash) ⇒ Mash Also known as: merge!
Returns The updated mash.
51 52 53 54 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 51 def update(other_hash) other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) } self end |
#values_at(*indices) ⇒ Array
Returns The values at each of the provided keys.
82 83 84 |
# File 'lib/gems/extlib-0.9.9/lib/extlib/mash.rb', line 82 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |