hash_util 1.3 → 1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/hash_helper.rb +65 -0
- data/lib/hash_util/version.rb +1 -1
- data/lib/hash_util.rb +16 -30
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0230c0efbf88089171c6e9a2235e91f2acaa9071
|
4
|
+
data.tar.gz: '08c22b3fd5ea8a65d8592b15b4d31d01ae9568ad'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ba88670cdb68c6b3f437248dfa6a0606d61315a03b4c97b82d724e3e0c3209ec60b00f8d20682316e4b8f003841799e83123fa865d78a4fba67d36a70e5cf8a1
|
7
|
+
data.tar.gz: 9b10d7350fb58bbe2c43f5973a322320ef1e4b9854f1e415b065d2f1d5136652cd276f43155b6180af7d615507c68cc88cf3629158921fd051e110daab6e29ab
|
data/README.md
CHANGED
data/lib/hash_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'hash_util/version'
|
2
|
+
require 'byebug'
|
3
|
+
|
4
|
+
# Ruby Hash utility module
|
5
|
+
module HashHelper
|
6
|
+
|
7
|
+
def tokenize(str)
|
8
|
+
# extract words + nums
|
9
|
+
str.scan(/[0-9.e-]+|\w+/)
|
10
|
+
end
|
11
|
+
|
12
|
+
# extracts all numbers in a hash string
|
13
|
+
def extract_numbers_hash(str)
|
14
|
+
str = tokenize str
|
15
|
+
# extract all nums including those using e notation
|
16
|
+
str.select! { |m| /^[0-9.e-]+$/.match m }
|
17
|
+
# used Float instead of to_f as to_f converts string to '0'
|
18
|
+
str.collect do |m|
|
19
|
+
Float m
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# submethod used by add_hash2_to_hash1 if type Array
|
24
|
+
def add_hash_if_array(a1, b1)
|
25
|
+
a1 = a1.enum_for(:each_with_index).collect do |m, index|
|
26
|
+
if %w[Array Hash].include?(m.class.name)
|
27
|
+
add_hash2_to_hash1(a1[index], b1[index])
|
28
|
+
else
|
29
|
+
m + b1[index]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
a1
|
33
|
+
end
|
34
|
+
|
35
|
+
# submethod used by add_hash2_to_hash1 if type Hash
|
36
|
+
def add_hash_if_hash(a1, b1)
|
37
|
+
a1.each do |k, _v|
|
38
|
+
if %w[Array Hash].include?(a1[k].class.name)
|
39
|
+
a1[k] = add_hash2_to_hash1(a1[k], b1[k])
|
40
|
+
else
|
41
|
+
a1[k] += b1[k]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# adds up hash2 values in to hash1
|
47
|
+
# alternate implementation using string instead of hash
|
48
|
+
|
49
|
+
# def self.add_hash2_to_hash1(hash1, hash2)
|
50
|
+
# token1 = hash1.to_s.scan(/[[+-]?([0-9]*[.])?[0-9e-]+]+|\w+|[{}\[\]:,"\040]/)
|
51
|
+
# token2 = extract_numbers_hash(hash2.to_s)
|
52
|
+
# j = 0
|
53
|
+
# token1.each_index do |i|
|
54
|
+
# begin
|
55
|
+
# Float token1[i]
|
56
|
+
# b = Float token2[j]
|
57
|
+
# token1[i] = token1[i].to_f + b
|
58
|
+
# j += 1
|
59
|
+
# rescue => e
|
60
|
+
# e.backtrace
|
61
|
+
# end
|
62
|
+
# end
|
63
|
+
# token1.join.gsub(/\s+/, ' ').gsub(/[.0]/, '')
|
64
|
+
# end
|
65
|
+
end
|
data/lib/hash_util/version.rb
CHANGED
data/lib/hash_util.rb
CHANGED
@@ -1,33 +1,30 @@
|
|
1
1
|
require 'hash_util/version'
|
2
2
|
require 'byebug'
|
3
|
+
require 'hash_helper'
|
3
4
|
|
4
5
|
# Ruby Hash utility module
|
5
6
|
module HashUtil
|
6
|
-
|
7
|
+
extend HashHelper
|
8
|
+
|
9
|
+
# adds hash2 value to hash1 value recursively
|
10
|
+
# alternate implementation using a string instead of hash
|
11
|
+
# can be found in hash helper module
|
7
12
|
def self.add_hash2_to_hash1(a1, b1)
|
8
13
|
if a1.class.name == 'Array'
|
9
|
-
a1
|
10
|
-
if %w[Array Hash].include?(m.class.name)
|
11
|
-
add_hash2_to_hash1(a1[index], b1[index])
|
12
|
-
else
|
13
|
-
m + b1[index]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
a1
|
14
|
+
add_hash_if_array(a1, b1)
|
17
15
|
elsif a1.class.name == 'Hash'
|
18
|
-
a1
|
19
|
-
if %w[Array Hash].include?(a1[k].class.name)
|
20
|
-
a1[k] = add_hash2_to_hash1(a1[k], b1[k])
|
21
|
-
else
|
22
|
-
a1[k] += b1[k]
|
23
|
-
end
|
24
|
-
end
|
16
|
+
add_hash_if_hash(a1, b1)
|
25
17
|
else
|
26
18
|
a1[k] += b1[k]
|
27
19
|
end
|
28
20
|
end
|
29
21
|
|
30
22
|
# set all values in the hash to 0
|
23
|
+
# This method should be moved to helper module and
|
24
|
+
# another genericmethod called set_Values to be added
|
25
|
+
# that can set all values including zero
|
26
|
+
# TODO optimize
|
27
|
+
# FIXME rubocop
|
31
28
|
def self.zero(obj)
|
32
29
|
if obj.class.name == 'Array'
|
33
30
|
obj = obj.collect do |m|
|
@@ -49,13 +46,15 @@ module HashUtil
|
|
49
46
|
# copies values from a hash string in to another
|
50
47
|
# The 2 hashes should be of same structure but keys
|
51
48
|
# can be different
|
49
|
+
# FIXME rubocop
|
52
50
|
def self.merge(hash_str1, hash_str2)
|
51
|
+
# extract nums, words
|
53
52
|
token1 = hash_str1.scan(/[[+-]?([0-9]*[.])?[0-9e-]+]+|\w+|[{}\[\]:,"\040]/)
|
54
53
|
token2 = extract_numbers_hash(hash_str2)
|
55
|
-
# byebug
|
56
54
|
j = 0
|
57
55
|
token1.each_index do |i|
|
58
56
|
begin
|
57
|
+
# used Float instead of to_f as to_f converts string to '0'
|
59
58
|
Float token1[i]
|
60
59
|
token1[i] = token2[j]
|
61
60
|
j += 1
|
@@ -64,17 +63,4 @@ module HashUtil
|
|
64
63
|
end
|
65
64
|
token1.join.gsub(/\s+/, ' ')
|
66
65
|
end
|
67
|
-
|
68
|
-
def self.tokenize(str)
|
69
|
-
str.scan(/[0-9.e-]+|\w+/)
|
70
|
-
end
|
71
|
-
|
72
|
-
# extracts all numbers in a hash string
|
73
|
-
def self.extract_numbers_hash(str)
|
74
|
-
str = tokenize str
|
75
|
-
str.select! { |m| /^[0-9.e-]+$/.match m }
|
76
|
-
str.collect do |m|
|
77
|
-
Float m
|
78
|
-
end
|
79
|
-
end
|
80
66
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_util
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '1.
|
4
|
+
version: '1.4'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- dinesh.theetla@gmail.com
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
71
|
- hash_util.gemspec
|
72
|
+
- lib/hash_helper.rb
|
72
73
|
- lib/hash_util.rb
|
73
74
|
- lib/hash_util/version.rb
|
74
75
|
homepage: https://github.com/dtheetla/hash_util
|