Class: Mash
Overview
This class has dubious semantics and we only have it so that people can write params[:key] instead of params['key'].
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
-
# ⇒ Object
If key is a Symbol and it is a key in the mash, then the default value will be set to the value matching the key.
- #delete(key) ⇒ Object
-
#fetch(key, *args) { ... } ⇒ Object
The value at key or the default value.
-
# ⇒ Mash
constructor
If constructor is a Hash, a new mash will be created based on the keys of the hash and no default value will be set.
-
#key?(key) ⇒ Boolean
(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.
-
#symbolize_keys ⇒ Hash
The mash as a Hash with symbolized keys.
-
#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
#extractable_options?, #to_mash, zip
Methods included from Gorillib::Hashlike::Serialization
Methods included from Gorillib::Hashlike::ReverseMerge
#reverse_merge, #reverse_merge!
Methods included from Gorillib::Hashlike::DeepCompact
Methods included from Gorillib::Hashlike::DeepMerge
Methods included from Gorillib::Hashlike::DeepDup
Methods included from Gorillib::Hashlike::Compact
#compact, #compact!, #compact_blank, #compact_blank!
Methods included from Gorillib::Hashlike::Slice
#except, #except!, #extract!, #only, #only!, #slice, #slice!
Methods included from Gorillib::Hashlike::Keys
#assert_valid_keys, #stringify_keys, #symbolize_keys!
Constructor Details
Instance Method Details
#[]=(key, value) ⇒ Object
56 57 58 |
# File 'lib/gorillib/hash/mash.rb', line 56 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
# ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/gorillib/hash/mash.rb', line 39 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end |
#delete(key) ⇒ Object
109 110 111 |
# File 'lib/gorillib/hash/mash.rb', line 109 def delete(key) super(convert_key(key)) end |
#fetch(key, *args) { ... } ⇒ Object
Returns The value at key or the default value.
88 89 90 |
# File 'lib/gorillib/hash/mash.rb', line 88 def fetch(key, *args, &block) super(convert_key(key), *args, &block) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
Returns True if the key exists in the mash.
75 76 77 |
# File 'lib/gorillib/hash/mash.rb', line 75 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Mash
Returns A new mash with the hash values merged in.
103 104 105 |
# File 'lib/gorillib/hash/mash.rb', line 103 def merge(hash) self.dup.update(hash) end |
#regular_update ⇒ Object
48 |
# File 'lib/gorillib/hash/mash.rb', line 48 alias_method :regular_update, :update |
#regular_writer ⇒ Object
47 |
# File 'lib/gorillib/hash/mash.rb', line 47 alias_method :regular_writer, :[]= |
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
116 |
# File 'lib/gorillib/hash/mash.rb', line 116 def stringify_keys!; self end |
#symbolize_keys ⇒ Hash
Returns The mash as a Hash with symbolized keys.
119 120 121 122 123 |
# File 'lib/gorillib/hash/mash.rb', line 119 def symbolize_keys hsh = Hash.new(default) each{|key, val| hsh[key.to_sym] = val } hsh end |
#to_hash ⇒ Hash
Returns The mash as a Hash with string keys.
126 127 128 |
# File 'lib/gorillib/hash/mash.rb', line 126 def to_hash Hash.new(default).merge(self) end |
#update(other_hash) ⇒ Mash Also known as: merge!
Returns The updated mash.
65 66 67 68 |
# File 'lib/gorillib/hash/mash.rb', line 65 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.
96 97 98 |
# File 'lib/gorillib/hash/mash.rb', line 96 def values_at(*indices) indices.map{|key| self[convert_key(key)] } end |