Module: HashUtil

Defined in:
lib/hash_util.rb,
lib/hash_util/version.rb

Overview

Ruby Hash utility module

Constant Summary collapse

VERSION =
"1.3"

Class Method Summary collapse

Class Method Details

.add_hash2_to_hash1(a1, b1) ⇒ Object

adds up hash2 values in to hash1



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hash_util.rb', line 7

def self.add_hash2_to_hash1(a1, b1)
  if a1.class.name == 'Array'
    a1 = a1.enum_for(:each_with_index).collect do |m, index|
      if %w[Array Hash].include?(m.class.name)
        add_hash2_to_hash1(a1[index], b1[index])
      else
        m + b1[index]
      end
    end
    a1
  elsif a1.class.name == 'Hash'
    a1.each do |k, _v|
      if %w[Array Hash].include?(a1[k].class.name)
        a1[k] = add_hash2_to_hash1(a1[k], b1[k])
      else
        a1[k] += b1[k]
      end
    end
  else
    a1[k] += b1[k]
  end
end

.extract_numbers_hash(str) ⇒ Object

extracts all numbers in a hash string



73
74
75
76
77
78
79
# File 'lib/hash_util.rb', line 73

def self.extract_numbers_hash(str)
  str = tokenize str
  str.select! { |m| /^[0-9.e-]+$/.match m }
  str.collect do |m|
    Float m
  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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hash_util.rb', line 52

def self.merge(hash_str1, hash_str2)
  token1 = hash_str1.scan(/[[+-]?([0-9]*[.])?[0-9e-]+]+|\w+|[{}\[\]:,"\040]/)
  token2 = extract_numbers_hash(hash_str2)
  # byebug
  j = 0
  token1.each_index do |i|
    begin
      Float token1[i]
      token1[i] = token2[j]
      j += 1
    rescue
    end
  end
  token1.join.gsub(/\s+/, ' ')
end

.tokenize(str) ⇒ Object



68
69
70
# File 'lib/hash_util.rb', line 68

def self.tokenize(str)
  str.scan(/[0-9.e-]+|\w+/)
end

.zero(obj) ⇒ Object

set all values in the hash to 0



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hash_util.rb', line 31

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