Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/invoca/utils/hash.rb

Overview

Invoca ::Hash extensions

Instance Method Summary collapse

Instance Method Details

#&(keys) ⇒ Object



37
38
39
40
41
# File 'lib/invoca/utils/hash.rb', line 37

def &(keys)
  res = {}
  keys.each { |k| res[k] = self[k] if has_key?(k) }
  res
end

#-(keys) ⇒ Object

rubocop:disable Naming/BinaryOperatorParameterName



31
32
33
34
35
# File 'lib/invoca/utils/hash.rb', line 31

def -(keys)
  res = {}
  each_pair { |k, v| res[k] = v unless k.in?(keys) }
  res
end

#map_hash(&block) ⇒ Object



11
12
13
14
15
# File 'lib/invoca/utils/hash.rb', line 11

def map_hash(&block)
  res = {}
  each { |k, v| res[k] = block.arity == 1 ? yield(v) : yield(k, v) }
  res
end

#partition_hash(keys = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/invoca/utils/hash.rb', line 17

def partition_hash(keys = nil)
  yes = {}
  no = {}
  each do |k, v|
    if block_given? ? yield(k, v) : keys.include?(k)
      yes[k] = v
    else
      no[k] = v
    end
  end
  [yes, no]
end

#select_hash(&block) ⇒ Object



5
6
7
8
9
# File 'lib/invoca/utils/hash.rb', line 5

def select_hash(&block)
  res = {}
  each { |k, v| res[k] = v if (block.arity == 1 ? yield(v) : yield(k, v)) } # rubocop:disable Style/ParenthesesAroundCondition
  res
end