hash_util 0.1.0 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4260305e402dc4de023b1f4690ce07bc8e1ae1dd
4
- data.tar.gz: 576fa1b0898e62410441e0e63e22187ad3e023fb
3
+ metadata.gz: 4e7bcbc1535ccb18c49cfd09076c942764bd314f
4
+ data.tar.gz: 8417454997da561f492a79e251fa894e4a102578
5
5
  SHA512:
6
- metadata.gz: 0a18e62ed1cb6df5df1ecb40bee4de73401f1607dc13b55e953fa19bca28db536657f3bcd44e448a77d47c701684babef7cc2fe9dc2b4f536a548e27282f3e45
7
- data.tar.gz: 36fec161426a1174e8c839aa083be7f5243881ca333ad375ba373bda2eff6d154b3bfdce9963cbae21216266d562e4da7c1a0004ce54dfb0ed5be5dc9611bff0
6
+ metadata.gz: b39ed7b7987778b2a5fd6d2d1911f2777abb1d44c248ea121f38de18189ea775f8192e18a8ea0f5e8f9d45e0700724a462764d12824ebcacee1ea6842eb530b0
7
+ data.tar.gz: aef50b34442a3fa01a6f743bc655221c064503d7374f01e7a7b890dc5fcc14ef467c8cef04166b87bb6c1f607f65c346eb77ba5b274ab47b722ab793f50226e8
data/README.md CHANGED
@@ -24,20 +24,55 @@ Or install it yourself as:
24
24
 
25
25
  ## Usage
26
26
 
27
- (1)add_hash2_to_hash1(hash1,hash2)
27
+ (1)add_hash2_to_hash1)
28
28
  ```ruby
29
29
  hash1 = {a:1,b:2,c:[1,2],d:{a:1}}
30
30
  hash2 = {a:3,b:3,c:[3,2],d:{a:10}}
31
- hash3 = HashUtil.add_hash2_to_hash1(hash1,hash2) # {a:4,b:5,c:[4,4],d:{a:11}}
31
+ hash3 = HashUtil.add_hash2_to_hash1(hash1,hash2) # hash1 = hash3 = {a:4,b:5,c:[4,4],d:{a:11}}
32
32
  ```
33
33
  (2) zero
34
34
  ```ruby
35
35
  hash1 = {a:1,b:2,c:[1,2],d:{a:1}}
36
36
  HashUtil.zero(hash1) #{a:0,b:0,c:[0,0],d:{a:0}}
37
37
  ```
38
+ (3) merge
39
+ 0verrides hash1 values with hash2 values.
40
+ ```ruby
41
+ str1 = %{
42
+ { "OccExp": [ -0.0004, 0.09600, 0.0000, -0.0204, 0.09 ,"abc":{"a":1,"b":0,"c":[0,1,2]} ],
43
+ "PremRV": [ 8.500, 4.19999981, 5.0, 7.80 ]}
44
+ }
45
+
46
+ str2 = %{
47
+ { "OccExp": [ 0.0250000004, 0.0, 0.023, 0.0250000004, 0.0280000009,
48
+ "abc":{"a":1,"b":0,"c":[0,1,2]} ], "PremRV": [ 8.5, 4.19999981, 5.5,
49
+ 7.80000019 ]}
50
+ }
51
+ str1 = HashUtil.merge(str1,str2) # values in str1 == str2
52
+ ```
53
+ (4) tokenize
54
+ tokenizes a hash and returns a flat array
55
+ ```ruby
56
+ str2 = %{
57
+ { "OccExp": [ 0.0250000004, 0.0199999996, "abc":{"a":1,"b":0,"c":[0,1,2]}]}
58
+ }
59
+ HashUtil.extract_numbers_hash(str2) #[ 0.0250000004, 0.0199999996,1,0,0,1,2]
60
+ ```
61
+ (5) extract_numbers_hash
62
+ extracts numbers from a hash
63
+ ```ruby
64
+ str2 = %{
65
+ { "OccExp": [ 0.0250000004, 0.0199999996, "abc":{"a":1,"b":0,"c":[0,1,2]}]}
66
+ }
67
+ HashUtil.extract_numbers_hash(str2) #[ 0.0250000004, 0.0199999996,1,0,0,1,2]
68
+
69
+ ```
70
+
71
+
72
+
38
73
  ## Contributing
39
74
 
40
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hash_util. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dtheetla/hash_util. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
41
76
 
42
77
  ## License
43
78
 
data/hash_util.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["dinesh"]
11
11
 
12
12
  spec.summary = %q{Ruby Hash utility methods.}
13
- spec.description = %q{Hash utility methods to add 2 hashes / set all hash values to zero}
13
+ spec.description = %q{Hash utility methods for Ruby Hashes, refer documentation}
14
14
  spec.homepage = "https://github.com/dtheetla/hash_util"
15
15
  spec.license = "MIT"
16
16
 
@@ -1,3 +1,3 @@
1
1
  module HashUtil
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0"
3
3
  end
data/lib/hash_util.rb CHANGED
@@ -1,47 +1,77 @@
1
- require "hash_util/version"
1
+ require 'hash_util/version'
2
+ require 'byebug'
2
3
 
4
+ # Ruby Hash utility module
3
5
  module HashUtil
4
6
  # adds up hash2 values in to hash1
5
- def self.add_hash2_to_hash1(a1,b1)
6
- if a1.class.name=="Array"
7
- a1= a1.enum_for(:each_with_index).collect{|m,index|
8
- if(['Array','Hash'].include?(m.class.name))
9
- add_hash2_to_hash1(a1[index],b1[index])
10
- else
7
+ def self.add_hash2_to_hash1(a1, b1)
8
+ if a1.class.name == 'Array'
9
+ a1 = a1.enum_for(:each_with_index).collect do |m, index|
10
+ if %w[Array Hash].include?(m.class.name)
11
+ add_hash2_to_hash1(a1[index], b1[index])
12
+ else
11
13
  m + b1[index]
12
14
  end
13
- }
14
- return a1
15
- elsif a1.class.name=="Hash"
16
- a1.each do |k,v|
17
- if ['Array','Hash'].include?(a1[k].class.name)
18
- a1[k]=add_hash2_to_hash1(a1[k],b1[k])
15
+ end
16
+ a1
17
+ elsif a1.class.name == 'Hash'
18
+ a1.each do |k, _v|
19
+ if %w[Array Hash].include?(a1[k].class.name)
20
+ a1[k] = add_hash2_to_hash1(a1[k], b1[k])
19
21
  else
20
22
  a1[k] += b1[k]
21
23
  end
22
24
  end
23
25
  else
24
- a1[k]+=b1[k]
26
+ a1[k] += b1[k]
25
27
  end
26
28
  end
27
29
 
28
30
  # set all values in the hash to 0
29
31
  def self.zero(obj)
30
- if obj.class.name=="Array"
31
- obj=obj.collect{|m|
32
- ['Array','Hash'].include?(m.class.name) ? zero(m) : 0
33
- }
34
- elsif obj.class.name=="Hash"
35
- obj.each do |k,v|
36
- if ['Array','Hash'].include?(obj[k].class.name)
37
- obj[k]=zero(obj[k])
38
- else
39
- obj[k]=0
40
- end
32
+ if obj.class.name == 'Array'
33
+ obj = obj.collect do |m|
34
+ %w[Array Hash].include?(m.class.name) ? zero(m) : 0
35
+ end
36
+ elsif obj.class.name == 'Hash'
37
+ obj.each do |k, _v|
38
+ obj[k] = if %w[Array Hash].include?(obj[k].class.name)
39
+ zero(obj[k])
40
+ else
41
+ 0
42
+ end
41
43
  end
42
44
  else
43
- obj[k]=0
45
+ obj[k] = 0
46
+ end
47
+ end
48
+
49
+ # copies values from a hash string in to another
50
+ # The 2 hashes should be of same structure but keys
51
+ # can be different
52
+ def self.merge(hash_str1, hash_str2)
53
+ token1 = hash_str1.scan(/[[+-]?([0-9]*[.])?[0-9]+]+|\w+|[{}\[\]:,"\040]/)
54
+ token2 = extract_numbers_hash(hash_str2)
55
+ j = 0
56
+ token1.each_index do |i|
57
+ if /[0-9.]/.match token1[i]
58
+ token1[i] = token2[j]
59
+ j += 1
60
+ end
44
61
  end
45
- end
62
+ token1.join.gsub(/\s+/, ' ')
63
+ end
46
64
 
65
+ def self.tokenize(str)
66
+ str.scan(/[0-9.-]+|\w+/)
67
+ end
68
+
69
+ # extracts all numbers in a hash string
70
+ def self.extract_numbers_hash(str)
71
+ str = tokenize str
72
+ str.select! { |m| /[0-9.]/.match m }
73
+ str.collect do |m|
74
+ m.to_s.include?('.') ? m.to_f : m.to_i
75
+ end
76
+ end
47
77
  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: 0.1.0
4
+ version: '1.0'
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-09-14 00:00:00.000000000 Z
11
+ date: 2017-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '5.0'
55
- description: Hash utility methods to add 2 hashes / set all hash values to zero
55
+ description: Hash utility methods for Ruby Hashes, refer documentation
56
56
  email:
57
57
  - dinesh
58
58
  executables: []