class Hash
Public Instance Methods
Adds the contents of other_hash
to hsh
. Entries with duplicate keys are overwritten with the values from other_hash
# File lib/mug/hash/operations.rb, line 24 def + other_hash merge other_hash end
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.
Example:
h = {} h << {:a=>0} # h = {:a=>0} h << {:b=>2,:c=>3} # h = {:a=>0,:b=>2,:c=>3} h << [:a,1] # h = {:a=>1,:b=>2,:c=>3}
# File lib/mug/hash/operations.rb, line 42 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 tap { 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
Returns a value from the hash for the given key. If the key can't be found, there are several options: if default is given, then that will be stored and returned; if the optional code block is specified, then that will be run and its result stored and returned.
# File lib/mug/hash/fetch-assign.rb, line 10 def fetch_assign key, *default raise ArgumentError, "wrong number of arguments (given #{default.length + 1}, expected 1..2)" if default.length > 1 store key, (default.length == 1 ? default[0] : yield(key)) unless key? key fetch key end
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}
# File lib/mug/hash/map.rb, line 39 def map_keys &_block # :yields: key return enum_for(:map_keys) unless block_given? hsh = {} each do |k, v| hsh[ yield k ] = v end hsh end
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
# File lib/mug/hash/map.rb, line 55 def map_keys! &block # :yields: key return enum_for(:map_keys!) unless block_given? replace map_keys(&block) end
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'}
# File lib/mug/hash/map.rb, line 70 def map_pairs &_block # :yields: key, value return enum_for(:map_pairs) unless block_given? hsh = {} each do |k, v| a, b = yield k, v hsh[a] = b end hsh end
Replaces the keys and values in hsh
by running them each through block
.
If block
returns duplicate keys, they will be overwritten.
See: map_pairs
# File lib/mug/hash/map.rb, line 87 def map_pairs! &block # :yields: key, value return enum_for(:map_pairs!) unless block_given? replace map_pairs(&block) end
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"}
# File lib/mug/hash/map.rb, line 10 def map_values &_block # :yields: value return enum_for(:map_values) unless block_given? hsh = {} each do |k, v| hsh[k] = yield v end hsh end
Replaces the values in hsh
by running them each through block
.
See: map_values
# File lib/mug/hash/map.rb, line 24 def map_values! &block # :yields: value return enum_for(:map_values!) unless block_given? replace map_values(&block) end
Returns a new hash containing the contents of other_hash
and the contents of hsh
. The value for each duplicate key is the value in hsh
when it exists.
# File lib/mug/hash/merge.rb, line 9 def merge_left other_hash merge(other_hash) {|key, left, right| left.nil? ? right : left } end
Adds the contents of other_hash
to hsh
. Entries with duplicate keys are overwritten with the values from other_hash
if the values in hsh
are nil
.
# File lib/mug/hash/merge.rb, line 27 def merge_left! other_hash merge!(other_hash) {|key, left, right| left.nil? ? right : left } end
Returns a new hash containing the contents of other_hash
and the contents of hsh
. The value for each duplicate key is the value in other_hash
when it exists.
# File lib/mug/hash/merge.rb, line 18 def merge_right other_hash merge(other_hash) {|key, left, right| right.nil? ? left : right } end
Adds the contents of other_hash
to hsh
. Entries with duplicate keys are overwritten with the values from other_hash
unless the values in other_hash
are nil
.
# File lib/mug/hash/merge.rb, line 36 def merge_right! other_hash merge!(other_hash) {|key, left, right| right.nil? ? left : right } end
Converts hsh to a boolean. Returns true if not empty.
# File lib/mug/bool.rb, line 76 def to_b !empty? end
Use a Hash
like a case statement.
case key when /foo/ then "FOO" when /bar/ then "BAR" else "DEFAULT" end
becomes:
h = { /foo/ => "FOO", /bar/ => "BAR", } h.default = "DEFAULT" h.when key
# File lib/mug/hash/when.rb, line 22 def when o each_pair do |k, v| return v if k === o end default o end
Returns a new Hash
, whose value is the same as this one, with any extras in other_hash
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}
# File lib/mug/hash/operations.rb, line 15 def | other_hash other_hash.merge self end