Class: Hash
Direct Known Subclasses
Origen::Clocks::ClocksCollection, Origen::Parameters::Set, Origen::Ports::PortCollection, Origen::PowerDomains::PowerDomainsCollection, Origen::Registers::RegCollection
Instance Method Summary collapse
-
#filter(filter) ⇒ Object
Filter a hash by a key filter of various types.
- #ids ⇒ Object
-
#intersect?(hash) ⇒ Boolean
Boolean method to check if self and another hash have intersecting keys.
-
#intersections(hash) ⇒ Object
Returns a hash containing the intersecting keys between self and another hash.
-
#longest_key ⇒ Object
Returns the longest key as measured by String#length.
-
#longest_value ⇒ Object
Returns the longest key as measured by String#length.
- #recursive_find_by_key(key) ⇒ Object
-
#update_common(hash) ⇒ Object
Only updates the common keys that exist in self.
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 |
# 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 filtered_hash = select do |k, v| [TrueClass, FalseClass].include?(select_logic.class) ? select_logic : !!eval(select_logic) end filtered_hash end |
#ids ⇒ Object
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
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_key ⇒ Object
Returns the longest key as measured by String#length
43 44 45 |
# File 'lib/origen/core_ext/hash.rb', line 43 def longest_key keys.map(&:to_s).max_by(&:length) end |
#longest_value ⇒ Object
Returns the longest key as measured by String#length
48 49 50 |
# File 'lib/origen/core_ext/hash.rb', line 48 def longest_value values.map(&:to_s).max_by(&:length) end |
#recursive_find_by_key(key) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/origen/core_ext/hash.rb', line 52 def recursive_find_by_key(key) search_results = {} # Used to store results when key is a Regexp # Create a stack of hashes to search through for the needle which # is initially this hash stack = [self] # So long as there are more haystacks to search... while (to_search = stack.pop) # ...keep searching for this particular key... to_search.each do |k, v| # If this value can be recursively searched... if v.respond_to?(:recursive_find_by_key) # ...push that on to the list of places to search. stack << v elsif key.is_a? Regexp search_results[k] = v if key.match(k.to_s) else return v if k == key end end end if search_results.empty? nil elsif search_results.size == 1 search_results.values.first else search_results end 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 |