Module: HashHelper

Included in:
HashUtil
Defined in:
lib/hash_helper.rb

Overview

Ruby Hash utility module

Instance Method Summary collapse

Instance Method Details

#add_hash_if_array(a1, b1) ⇒ Object

submethod used by add_hash2_to_hash1 if type Array



14
15
16
17
18
19
20
21
22
23
# File 'lib/hash_helper.rb', line 14

def add_hash_if_array(a1, b1)
  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
end

#add_hash_if_hash(a1, b1) ⇒ Object

submethod used by add_hash2_to_hash1 if type Hash



26
27
28
29
30
31
32
33
34
# File 'lib/hash_helper.rb', line 26

def add_hash_if_hash(a1, b1)
  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
end

#tokenize(str) ⇒ Object



7
8
9
10
# File 'lib/hash_helper.rb', line 7

def tokenize(str)
  # extract words + nums
  str.scan(/[0-9.e-]+|\w+/)
end