Module: HashUtil
- Extended by:
- HashHelper
- Defined in:
- lib/hash_util.rb,
lib/hash_util/version.rb
Overview
Ruby Hash utility module
Constant Summary collapse
- VERSION =
"1.4"
Class Method Summary collapse
-
.add_hash2_to_hash1(a1, b1) ⇒ Object
adds hash2 value to hash1 value recursively alternate implementation using a string instead of hash can be found in hash helper module.
-
.merge(hash_str1, hash_str2) ⇒ Object
copies values from a hash string in to another The 2 hashes should be of same structure but keys can be different FIXME rubocop.
-
.zero(obj) ⇒ Object
set all values in the hash to 0 This method should be moved to helper module and another genericmethod called set_Values to be added that can set all values including zero TODO optimize FIXME rubocop.
Methods included from HashHelper
add_hash_if_array, add_hash_if_hash, extract_numbers_hash, tokenize
Class Method Details
.add_hash2_to_hash1(a1, b1) ⇒ Object
adds hash2 value to hash1 value recursively alternate implementation using a string instead of hash can be found in hash helper module
12 13 14 15 16 17 18 19 20 |
# File 'lib/hash_util.rb', line 12 def self.add_hash2_to_hash1(a1, b1) if a1.class.name == 'Array' add_hash_if_array(a1, b1) elsif a1.class.name == 'Hash' add_hash_if_hash(a1, b1) else a1[k] += b1[k] end end |
.merge(hash_str1, hash_str2) ⇒ Object
copies values from a hash string in to another The 2 hashes should be of same structure but keys can be different FIXME rubocop
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/hash_util.rb', line 50 def self.merge(hash_str1, hash_str2) # extract nums, words token1 = hash_str1.scan(/[[+-]?([0-9]*[.])?[0-9e-]+]+|\w+|[{}\[\]:,"\040]/) token2 = extract_numbers_hash(hash_str2) j = 0 token1.each_index do |i| begin # used Float instead of to_f as to_f converts string to '0' Float token1[i] token1[i] = token2[j] j += 1 rescue end end token1.join.gsub(/\s+/, ' ') end |
.zero(obj) ⇒ Object
set all values in the hash to 0
This method should be moved to helper module and
another genericmethod called set_Values to be added
that can set all values including zero
TODO optimize
FIXME rubocop
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hash_util.rb', line 28 def self.zero(obj) if obj.class.name == 'Array' obj = obj.collect do |m| %w[Array Hash].include?(m.class.name) ? zero(m) : 0 end elsif obj.class.name == 'Hash' obj.each do |k, _v| obj[k] = if %w[Array Hash].include?(obj[k].class.name) zero(obj[k]) else 0 end end else obj[k] = 0 end end |