Class: Hash

Inherits:
Object show all
Defined in:
lib/mug/bool.rb,
lib/mug/hash/map.rb,
lib/mug/hash/when.rb,
lib/mug/hash/merge.rb,
lib/mug/hash/operations.rb,
lib/mug/hash/fetch-assign.rb

Instance Method Summary collapse

Instance Method Details

#+(other_hash) ⇒ Hash

Adds the contents of other_hash to hsh. Entries with duplicate keys are overwritten with the values from other_hash

Parameters:

  • other_hash (Hash)

    the hash to add

Returns:

  • (Hash)

    a new merged hash



30
31
32
# File 'lib/mug/hash/operations.rb', line 30

def + other_hash
  merge other_hash
end

#<<(o) ⇒ Hash

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

Parameters:

  • o (Hash, Array, #to_h)

    the object to append

Returns:

  • (Hash)

    hsh after appending

Raises:

  • (ArgumentError)

    if o cannot be appended



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mug/hash/operations.rb', line 52

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

#fetch_assign(key, *default) ⇒ Object Also known as: compute_if_absent

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.

Examples:

require 'mug/hash/fetch-assign'
hsh = {}
hsh.fetch_assign(:a, 1) #=> 1
# hsh => {:a => 1}
hsh.fetch_assign(:a, 42) #=> 1
# hsh => {:a => 1}
hsh.fetch_assign(:b) {|key| key.to_s } #=> "b"
# hsh => {:a => 1, :b => "b"}

Parameters:

  • key (Object)

    the key to look up

  • default (Object)

    the default value to store and return if the key is missing

Returns:

  • (Object)

    the value for the given key

Raises:

  • (ArgumentError)


24
25
26
27
28
# File 'lib/mug/hash/fetch-assign.rb', line 24

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

#map_keys(&_block) ⇒ Hash, Enumerator

Note:

As of Ruby 2.5 this is equivalent to #transform_keys

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}

Returns:

  • (Hash)

    a new hash with transformed keys

  • (Enumerator)

    if no block is given



53
54
55
56
57
58
59
60
# File 'lib/mug/hash/map.rb', line 53

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

#map_keys!(&block) ⇒ Hash, Enumerator

Note:

As of Ruby 2.5 this is equivalent to #transform_keys!

Replaces the keys in hsh by running them each through block.

If block returns duplicate keys, they will be overwritten in turn.

Returns:

  • (Hash)

    hsh with keys replaced

  • (Enumerator)

    if no block is given

See Also:



73
74
75
76
# File 'lib/mug/hash/map.rb', line 73

def map_keys! &block # :yields: key
  return enum_for(:map_keys!) unless block_given?
  replace map_keys(&block)
end

#map_pairs(&_block) ⇒ Hash, Enumerator

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'}

Returns:

  • (Hash)

    a new hash with transformed key-value pairs

  • (Enumerator)

    if no block is given



91
92
93
94
95
96
97
98
99
# File 'lib/mug/hash/map.rb', line 91

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

#map_pairs!(&block) ⇒ Hash, Enumerator

Replaces the keys and values in hsh by running them each through block.

If block returns duplicate keys, they will be overwritten.

Returns:

  • (Hash)

    hsh with keys and values replaced

  • (Enumerator)

    if no block is given

See Also:



110
111
112
113
# File 'lib/mug/hash/map.rb', line 110

def map_pairs! &block # :yields: key, value
  return enum_for(:map_pairs!) unless block_given?
  replace map_pairs(&block)
end

#map_values(&_block) ⇒ Hash, Enumerator

Note:

As of Ruby 2.4 this is equivalent to #transform_values

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"}

Returns:

  • (Hash)

    a new hash with transformed values

  • (Enumerator)

    if no block is given



15
16
17
18
19
20
21
22
# File 'lib/mug/hash/map.rb', line 15

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

#map_values!(&block) ⇒ Hash, Enumerator

Note:

As of Ruby 2.4 this is equivalent to #transform_values!

Replaces the values in hsh by running them each through block.

Returns:

  • (Hash)

    hsh with values replaced

  • (Enumerator)

    if no block is given

See Also:



33
34
35
36
# File 'lib/mug/hash/map.rb', line 33

def map_values! &block # :yields: value
  return enum_for(:map_values!) unless block_given?
  replace map_values(&block)
end

#merge_left(other_hash) ⇒ Hash

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.

Parameters:

  • other_hash (Hash)

    the hash to merge with

Returns:

  • (Hash)

    a new merged hash



12
13
14
# File 'lib/mug/hash/merge.rb', line 12

def merge_left other_hash
  merge(other_hash) {|key, left, right| left.nil? ? right : left }
end

#merge_left!(other_hash) ⇒ Hash

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.

Parameters:

  • other_hash (Hash)

    the hash to merge with

Returns:

  • (Hash)

    hsh after merging



36
37
38
# File 'lib/mug/hash/merge.rb', line 36

def merge_left! other_hash
  merge!(other_hash) {|key, left, right| left.nil? ? right : left }
end

#merge_right(other_hash) ⇒ Hash

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.

Parameters:

  • other_hash (Hash)

    the hash to merge with

Returns:

  • (Hash)

    a new merged hash



24
25
26
# File 'lib/mug/hash/merge.rb', line 24

def merge_right other_hash
  merge(other_hash) {|key, left, right| right.nil? ? left : right }
end

#merge_right!(other_hash) ⇒ Hash

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.

Parameters:

  • other_hash (Hash)

    the hash to merge with

Returns:

  • (Hash)

    hsh after merging



48
49
50
# File 'lib/mug/hash/merge.rb', line 48

def merge_right! other_hash
  merge!(other_hash) {|key, left, right| right.nil? ? left : right }
end

#to_bBoolean

Converts hsh to a boolean. Returns true if not empty.

Returns:

  • (Boolean)

    true if self is non-empty



124
125
126
# File 'lib/mug/bool.rb', line 124

def to_b
  !empty?
end

#when(o) ⇒ Object

Use a Hash like a case statement.

Returns the value whose key matches o using ===, or the default value.

becomes:

h = {
/foo/ => "FOO",
/bar/ => "BAR",
}
h.default = "DEFAULT"
h.when key

Parameters:

  • o (Object)

    the object to match against hash keys

Returns:

  • (Object)

    the value for the matching key, or the default value

    case key when /foo/ then "FOO" when /bar/ then "BAR" else "DEFAULT" end



28
29
30
31
32
33
# File 'lib/mug/hash/when.rb', line 28

def when o
  each_pair do |k, v|
    return v if k === o
  end
  default o
end

#|(other_hash) ⇒ Hash

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 = :b=>2, :x=>9

Parameters:

  • other_hash (Hash)

    the hash providing default values

Returns:

  • (Hash)

    a new hash with defaults applied



18
19
20
# File 'lib/mug/hash/operations.rb', line 18

def | other_hash
  other_hash.merge self
end