Class: Hash
- Defined in:
- lib/mug/bool.rb,
lib/mug/hashop.rb,
lib/mug/hashmap.rb
Instance Method Summary collapse
-
#<<(o) ⇒ Object
Appends stuff to the hash.
-
#map_keys(&block) ⇒ Object
Returns a new hash which is a copy of the current hash but each key is replaced by the result of running it through
block. -
#map_keys!(&block) ⇒ Object
Replaces the keys in
hshby running them each throughblock. -
#map_pairs(&block) ⇒ Object
Returns a new hash which is a copy of the current hash but each key-value pair is replaced by the result of running it through
block. -
#map_pairs!(&block) ⇒ Object
Replaces the values in
hshby running them each throughblock. -
#map_values(&block) ⇒ Object
Returns a new hash which is a copy of the current hash but each value is replaced by the result of running it through
block. -
#map_values!(&block) ⇒ Object
Replaces the values in
hshby running them each throughblock. -
#to_b ⇒ Object
Converts hsh to a boolean.
-
#|(o) ⇒ Object
Returns a new Hash, whose value is the same as this one, with any extras in
oadded in.
Instance Method Details
#<<(o) ⇒ Object
Appends stuff to the hash.
If o is a Hash, this is identical to calling #merge! If o is an Array with two elements, it is interpreted as [key,value] If o can be converted to a hash with #to_h, this is identical to calling #merge! Otherwise an ArgumentError is raised.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mug/hashop.rb', line 27 def << o if o.respond_to? :to_hash merge! o.to_hash elsif o.respond_to?(:to_a) && (a = o.to_a) && a.length == 2 store a[0], a[1] elsif o.respond_to? :to_h merge! o.to_h else raise ArgumentError, "#{o.class.name} is not a Hash" end end |
#map_keys(&block) ⇒ Object
Returns a new hash which is a copy of the current hash but each key is replaced by the result of running it through block.
If block returns duplicate keys, they will be overwritten in the resulting hash.
{'a'=>1, 'b'=>2}.map_keys { |k| k*2 } #=> {'aa'=>1, 'bb'=>2}
{'a'=>1, 'b'=>2}.map_keys { "cat" } #=> {'cat'=>2}
37 38 39 40 41 42 43 |
# File 'lib/mug/hashmap.rb', line 37 def map_keys &block # :yields: key hsh = {} each do |k, v| hsh[ yield k ] = v end hsh end |
#map_keys!(&block) ⇒ Object
Replaces the keys in hsh by running them each through block.
If block returns duplicate keys, they will be overwritten in turn.
See: #map_keys
52 53 54 |
# File 'lib/mug/hashmap.rb', line 52 def map_keys! &block # :yields: key replace map_keys(&block) end |
#map_pairs(&block) ⇒ Object
Returns a new hash which is a copy of the current hash but each key-value pair is replaced by the result of running it through block.
If block returns duplicate keys, they will be overwritten in the resulting hash.
{'a'=>1, 'b'=>2}.map_pairs { |k,v| [k*2, v+1] } #=> {'aa'=>2, 'bb'=>3}
{'a'=>1, 'b'=>2}.map_pairs { ["cat","dog"] } #=> {'cat'=>'dog'}
66 67 68 69 70 71 72 73 |
# File 'lib/mug/hashmap.rb', line 66 def map_pairs &block # :yields: key, value hsh = {} each do |k, v| a, b = yield k, v hsh[a] = b end hsh end |
#map_pairs!(&block) ⇒ Object
Replaces the values in hsh by running them each through block.
See: #map_values
80 81 82 |
# File 'lib/mug/hashmap.rb', line 80 def map_pairs! &block # :yields: key, value replace map_pairs(&block) end |
#map_values(&block) ⇒ Object
Returns a new hash which is a copy of the current hash but each value is replaced by the result of running it through block.
{'a'=>1, 'b'=>2}.map_values { |v| v*2 } #=> {'a'=>2, 'b'=>4}
{'a'=>1, 'b'=>2}.map_values { "cat" } #=> {'a'=>"cat", 'b'=>"cat"}
10 11 12 13 14 15 16 |
# File 'lib/mug/hashmap.rb', line 10 def map_values &block # :yields: value hsh = {} each do |k, v| hsh[k] = yield v end hsh end |
#map_values!(&block) ⇒ Object
Replaces the values in hsh by running them each through block.
See: #map_values
23 24 25 |
# File 'lib/mug/hashmap.rb', line 23 def map_values! &block # :yields: value replace map_values(&block) end |
#to_b ⇒ Object
Converts hsh to a boolean. Returns true if not empty.
76 77 78 |
# File 'lib/mug/bool.rb', line 76 def to_b !empty? end |
#|(o) ⇒ Object
Returns a new Hash, whose value is the same as this one, with any extras in o added in.
Useful for default options.
Example:
opts = {:a => 1, :b => 2 }
dflt = {:a => 0, :x => 9 }
opts |= dflt # => opts = {:a=>1, :b=>2, :x=>9}
15 16 17 |
# File 'lib/mug/hashop.rb', line 15 def | o o.merge self end |