Class: Grendel::Mash
- Inherits:
-
Hash
- Object
- Hash
- Grendel::Mash
- Defined in:
- lib/grendel/mash.rb
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #default(key = nil) ⇒ Object
- #delete(key) ⇒ Object
-
#dup ⇒ Mash
A duplicate of this mash.
-
#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.
Constructor Details
#initialize(constructor = {}) ⇒ Mash
Returns a new instance of Mash.
15 16 17 18 19 20 21 22 |
# File 'lib/grendel/mash.rb', line 15 def initialize(constructor = {}) if constructor.is_a?(Hash) super() update(constructor) else super(constructor) end end |
Instance Method Details
#[]=(key, value) ⇒ Object
46 47 48 |
# File 'lib/grendel/mash.rb', line 46 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#default(key = nil) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/grendel/mash.rb', line 29 def default(key = nil) if key.is_a?(Symbol) && include?(key = key.to_s) self[key] else super end end |
#delete(key) ⇒ Object
104 105 106 |
# File 'lib/grendel/mash.rb', line 104 def delete(key) super(convert_key(key)) end |
#dup ⇒ Mash
Returns A duplicate of this mash.
91 92 93 |
# File 'lib/grendel/mash.rb', line 91 def dup Mash.new(self) end |
#fetch(key, *extras) ⇒ Object
Returns The value at key or the default value.
78 79 80 |
# File 'lib/grendel/mash.rb', line 78 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.
65 66 67 |
# File 'lib/grendel/mash.rb', line 65 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Mash
Returns A new mash with the hash values merged in.
98 99 100 |
# File 'lib/grendel/mash.rb', line 98 def merge(hash) self.dup.update(hash) end |
#regular_update ⇒ Object
38 |
# File 'lib/grendel/mash.rb', line 38 alias_method :regular_update, :update |
#regular_writer ⇒ Object
37 |
# File 'lib/grendel/mash.rb', line 37 alias_method :regular_writer, :[]= |
#stringify_keys! ⇒ Mash
Used to provide the same interface as Hash.
111 |
# File 'lib/grendel/mash.rb', line 111 def stringify_keys!; self end |
#to_hash ⇒ Hash
Returns The mash as a Hash with string keys.
114 115 116 |
# File 'lib/grendel/mash.rb', line 114 def to_hash Hash.new(default).merge(self) end |
#update(other_hash) ⇒ Mash Also known as: merge!
Returns The updated mash.
55 56 57 58 |
# File 'lib/grendel/mash.rb', line 55 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.
86 87 88 |
# File 'lib/grendel/mash.rb', line 86 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |