numeric_with_unit 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/numeric_with_unit/unit.rb +12 -9
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a9e3bf253a2b7b59618f970d6c3dbdd22ff33b7
4
- data.tar.gz: 13adf31a8364d7ffa4e88c283ba0b67738c114e9
3
+ metadata.gz: d291b9eeb33681e615cf968e9ebf04564dea470b
4
+ data.tar.gz: cfbd75a22e7f4ed618127772d481a18582461c13
5
5
  SHA512:
6
- metadata.gz: 5938fcabeb8013050901f66e98888a78a02ab65fb175b3341747e91913044b570efe5c3d18a36683a388ab341b05877759200824e269b26bbe78e744e932434f
7
- data.tar.gz: 1154f4a257116d39df130e411e9c3021bbb4519ec851d76d345f43405cf3a5345d1ad7680e4d0d4b9d9e4123a2e9f4f06e1c36e5e1a04c7fa1cc1fd2e4be1f04
6
+ metadata.gz: 596b5e89d9161c75f00e49fb392cbaa8ec2d7ab272b174443671d28e5b3c961dc68dc8fd822b335a4adc4c62457cc370c235b6a4d4c587640617726bda0797b3
7
+ data.tar.gz: ca6f084096c236d63b2e327dc11795dc30d0043704ec8b752b7729ab4fcd80beab44db11d3a1d8c502b9535b56e9c4205926d36eaf8aee3da5d48ee55f195e8c
@@ -109,24 +109,25 @@ class NumericWithUnit
109
109
  def self.derive
110
110
  derivation = Hash.new(0)
111
111
  yield(derivation)
112
-
113
112
  return Unit.new if derivation.empty?
114
-
115
- dimension = Hash.new(0)
116
-
117
- h = derivation.sort_by{|u,v| u.symbol}.sort_by{|u,v| v} # ←どうしよう
113
+ derivation.delete_if{|k,v| k.symbol.nil?}
118
114
 
119
- syms_pos = h.select{|u,v| v > 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}
120
- syms_neg = h.select{|u,v| v < 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}
115
+ # constructing symbol
116
+ h = derivation.reject{|k,v| k.symbol.empty?}.sort_by{|u,v| u.symbol}.sort_by{|u,v| v}
117
+ syms_pos = h.select{|u,v| v > 0}.map{|u,v| u.symbol + (v.abs>1 ? v.abs.to_s : '')}
118
+ syms_neg = h.select{|u,v| v < 0}.map{|u,v| u.symbol + (v.abs>1 ? v.abs.to_s : '')}
121
119
  symbol = syms_pos.join('.')
122
120
  symbol += '/' + (syms_neg.size==1 ? "#{syms_neg.first}" : "(#{syms_neg.join('.')})") unless syms_neg.empty?
123
121
 
122
+ # constructing dimension
123
+ dimension = Hash.new(0)
124
124
  derivation.each do |u,v|
125
125
  u.dimension.each do |d,i|
126
126
  dimension[d] += i*v
127
127
  end
128
128
  end
129
-
129
+
130
+ # constructing from_si proc
130
131
  from_si = derivation.map{|u,v|
131
132
  prc = if v > 0
132
133
  ->(x){u.from_si(x)}
@@ -140,6 +141,7 @@ class NumericWithUnit
140
141
  ->(x){memo[prc[x]]}
141
142
  }
142
143
 
144
+ # constructing to_si proc
143
145
  to_si = derivation.map{|u,v|
144
146
  prc = if v > 0
145
147
  ->(x){u.to_si(x)}
@@ -152,7 +154,8 @@ class NumericWithUnit
152
154
  }.reduce{|memo, prc|
153
155
  ->(x){memo[prc[x]]}
154
156
  }
155
-
157
+
158
+ # deriving new unit
156
159
  self.new(derivation){|config|
157
160
  config.symbol = symbol
158
161
  config.dimension = dimension
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numeric_with_unit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - diaphragm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem provide NumericWithUnit class to calculate numeric with unit
14
14
  of measurement.