Module: HashExtension

Included in:
Hash
Defined in:
lib/oha_extensions/hash_extension.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



97
98
99
# File 'lib/oha_extensions/hash_extension.rb', line 97

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#assert_required_keys(*required_keys) ⇒ Object Also known as: must_include

If keys does not include everything in required_keys, throw an ArgumentError.

required_keys: list of keys that must be present for the hash (if a required key hashes to nil, but is passed it is still valid.

Example

=> nil.assert_required_keys(:happy) #will not raise error

required_keys may be passed as strings, symbols or both. Likewise the keys in the has maybe a strings or symbols or both.

Example

=> ‘bunny’.assert_required_keys(:happy) #will not raise error => ‘bunny’.assert_required_keys(:happy) #will not raise error

Raises

ArgumentError

if a required key is missing

Raises:

  • (ArgumentError)


86
87
88
89
# File 'lib/oha_extensions/hash_extension.rb', line 86

def assert_required_keys(*required_keys)
  keys_not_passed = [required_keys].flatten.map { |required_key| required_key.to_s } - self.stringify_keys.keys
  raise(ArgumentError, "The following keys were nil when expected not to be: #{keys_not_passed.join(", ")}") unless keys_not_passed.empty?
end

#increment(key, amount = 1, &block) ⇒ Object

Increments the value pointed to from the key If the key is nil then it is initialized to amount An optional block may be given, which is passed key, and each key in the hash. The block should return true for the key you wish to increment. Examples: h = Hash.new #h = nil h.increment(‘foo’) #1 h.increment(‘foo’) #2

age = Hash.new age[‘0 - 18’] = 0 age[‘18 - 24’] = 0 age[‘24 - 99999’] = 0

#increment the key where 18 is in range of the min and max age.increment(18) do |v, e|

min, max = e.split(' - ')
v >= min.to_i && v < max.to_i

end

assert_equal(0, age[‘0 - 18’]) assert_equal(1, age[‘18 - 24’]) assert_equal(0, age[‘24 - 99999’])



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/oha_extensions/hash_extension.rb', line 47

def increment(key, amount=1, &block)
  if block.nil?
    key_to_use = key
  else
    key_to_use = self.keys.detect { |k| block.call(key, k) } 
  end
  
  if self[key_to_use].nil?
    self[key_to_use] = amount
  else
    self[key_to_use] += amount
  end
end

#percent(key, &block) ⇒ Object

returns

What percent of the sum of the hash is the value for key

Examples: => 1, :b => 2, :c => 3, :d => 4.percent(:c) #0.30 because 3 is 30% of 10 and 10 is the total of the hash => [‘foo’, 1], :b => [‘monkey’, 2], :c => [‘bunny’, 3], :d => [‘kitty’, 4].percent(:c) { |e| e } #0.30 => 0 #0.0, if the sum of the hash is 0, then 0 is returned



66
67
68
69
70
71
72
73
# File 'lib/oha_extensions/hash_extension.rb', line 66

def percent(key, &block)
  block = lambda { |v| v } if block.nil?
  sum_block = lambda { |k, v| block.call(v) }
  
  total = self.sum(&sum_block)
  return 0 if total == 0
  return block.call(self[key]) / total.to_f
end

#select_pairs(&block) ⇒ Object



93
94
95
# File 'lib/oha_extensions/hash_extension.rb', line 93

def select_pairs(&block)
  return Hash[*self.select(&block).flatten]
end

#sum(&block) ⇒ Object

returns

The sum of each element in the hash

An optional block can be given which takes two parameters, key and value for each element in the hash. The return value of the block is summed for each element in the hash. If the result of sum is nil, 0 is returned Examples: => 1, :b => 2, :c => 3.sum #returns 6 => [‘foo’, 1], :b => [‘monkey’, 2], :c => [‘bunny’, 3].sum { |k, v| v } #returns 6 => [‘foo’, 1], :b => [‘monkey’, 2], :c => [‘bunny’, 3].sum { |k, v| v } #returns [‘foo’, 1, ‘monkey’, 2, ‘bunny’, 3] Order of elements is NOT guaranteed {}.sum #returns 0



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/oha_extensions/hash_extension.rb', line 13

def sum(&block)
  block = lambda { |k, v| v } if block.nil?
  
  total = nil
  
  each do |k, v| 
    total.nil? ? total = block.call(k, v) : total += block.call(k, v)
  end

  return total || 0
end