Class: Hash

Inherits:
Object show all
Defined in:
lib/origen/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#filter(filter) ⇒ Object

Filter a hash by a key filter of various types



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/origen/core_ext/hash.rb', line 26

def filter(filter)
  filtered_hash = {}
  select_logic = case filter
    when String then 'k[Regexp.new(filter)]'
    when (Fixnum || Integer || Float || Numeric) then "k[Regexp.new('#{filter}')]"
    when Regexp then 'k[filter]'
    when Symbol then 'k == filter'
    when NilClass then true
    else true
  end
  # rubocop:disable UnusedBlockArgument
  filtered_hash = select do |k, v|
    [TrueClass, FalseClass].include?(select_logic.class) ? select_logic : !!eval(select_logic)
  end
  filtered_hash
end

#idsObject



4
5
6
# File 'lib/origen/core_ext/hash.rb', line 4

def ids
  keys
end

#intersect?(hash) ⇒ Boolean

Boolean method to check if self and another hash have intersecting keys

Returns:

  • (Boolean)


16
17
18
# File 'lib/origen/core_ext/hash.rb', line 16

def intersect?(hash)
  (keys.to_set & hash.keys.to_set).empty? ? false : true
end

#intersections(hash) ⇒ Object

Returns a hash containing the intersecting keys between self and another hash



21
22
23
# File 'lib/origen/core_ext/hash.rb', line 21

def intersections(hash)
  (keys.to_set & hash.keys.to_set).to_a
end

#longest_keyObject

Returns the longest key as measured by String#length



44
45
46
# File 'lib/origen/core_ext/hash.rb', line 44

def longest_key
  keys.map(&:to_s).max_by(&:length)
end

#longest_valueObject

Returns the longest key as measured by String#length



49
50
51
# File 'lib/origen/core_ext/hash.rb', line 49

def longest_value
  values.map(&:to_s).max_by(&:length)
end

#update_common(hash) ⇒ Object

Only updates the common keys that exist in self



9
10
11
12
13
# File 'lib/origen/core_ext/hash.rb', line 9

def update_common(hash)
  each_key do |k|
    self[k] = hash[k] if hash.key? k
  end
end