numeric_with_unit 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/numeric_with_unit/unit.rb +175 -111
- data/lib/numeric_with_unit/{base_unit.rb → unit_definition/base.rb} +0 -2
- data/lib/numeric_with_unit/{cgs_unit.rb → unit_definition/cgs.rb} +1 -1
- data/lib/numeric_with_unit/{common_unit.rb → unit_definition/common.rb} +5 -2
- data/lib/numeric_with_unit/{imperial_unit.rb → unit_definition/imperial.rb} +1 -1
- data/lib/numeric_with_unit/{natural_unit.rb → unit_definition/natural.rb} +1 -1
- data/lib/numeric_with_unit/util2.rb +6 -6
- data/lib/numeric_with_unit.rb +11 -4
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f33f4241debd10d0f422dffd58afaa2ea4ff81bb
|
4
|
+
data.tar.gz: d75d05e2524f9f840a9019ed7cb17b2f0acd7071
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7edf5db65fff4bcd7dffbc9e035da919f60a079ca2583a95fd6acb47019ec3f7b11738ad728ff6117d33ca451b9d97971f2e246ba6772625bcc89274bb66d97e
|
7
|
+
data.tar.gz: f23d463f342e2d2ed4b1d0dc8b674b3e71030041e3500ff7740299ea0458856d6baec99a9a9d3fa5a2cee44549eed882f14a56aa862baa24e69220748b3e1a85
|
@@ -5,14 +5,13 @@ class NumericWithUnit
|
|
5
5
|
class Unit
|
6
6
|
class Config
|
7
7
|
attr_reader :symbol, :dimension, :derivation
|
8
|
-
attr_reader :si
|
8
|
+
attr_reader :si
|
9
9
|
|
10
10
|
def initialize(parent=nil)
|
11
11
|
@symbol = nil
|
12
12
|
@dimension = Hash.new(0)
|
13
13
|
@from_si = nil
|
14
14
|
@to_si = nil
|
15
|
-
@derivation = Hash.new(0)
|
16
15
|
@si = false
|
17
16
|
|
18
17
|
@parent = parent
|
@@ -20,52 +19,12 @@ class NumericWithUnit
|
|
20
19
|
|
21
20
|
def compile
|
22
21
|
@dimension.delete_if{|k,v| v.zero?}
|
23
|
-
@derivation.delete_if{|k,v| v.zero?}
|
24
|
-
@derivation.delete_if{|k,v| k.symbol.nil?}
|
25
22
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
h = @derivation.sort_by{|u,v| u.symbol}.sort_by{|u,v| v} # ←どうしよう
|
32
|
-
|
33
|
-
s1 = h.select{|u,v| v > 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}.join('.')
|
34
|
-
s2 = h.select{|u,v| v < 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}.join('.')
|
35
|
-
@symbol = s1 + (s2.empty? ? '' : "/(#{s2})")
|
36
|
-
|
37
|
-
@derivation.each do |u,v|
|
38
|
-
u.dimension.each do |d,i|
|
39
|
-
@dimension[d] += i*v
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
@from_si = @derivation.map{|u,v|
|
44
|
-
prc = if v > 0
|
45
|
-
->(x){u.from_si(x)}
|
46
|
-
else
|
47
|
-
->(x){x.quo(u.from_si(1)-u.from_si(0))} # ℃とKの変換のような場合に、変換式の切片を消すため。変換式が線形じゃないケースは想定していない
|
48
|
-
end
|
49
|
-
[prc, v.abs]
|
50
|
-
}.map{|prc,v|
|
51
|
-
->(x){ v.times{x = prc[x]}; x }
|
52
|
-
}.reduce{|memo, prc|
|
53
|
-
->(x){memo[prc[x]]}
|
54
|
-
}
|
55
|
-
|
56
|
-
@to_si = @derivation.map{|u,v|
|
57
|
-
prc = if v > 0
|
58
|
-
->(x){u.to_si(x)}
|
59
|
-
else
|
60
|
-
->(x){x.quo(u.to_si(1)-u.to_si(0))} # ℃とKの変換のような場合に、変換式の切片を消すため。変換式が線形じゃないケースは想定していない
|
61
|
-
end
|
62
|
-
[prc, v.abs]
|
63
|
-
}.map{|prc,v|
|
64
|
-
->(x){ v.times{x = prc[x]}; x }
|
65
|
-
}.reduce{|memo, prc|
|
66
|
-
->(x){memo[prc[x]]}
|
67
|
-
}
|
68
|
-
end
|
23
|
+
@from_si ||= ->(x){x}
|
24
|
+
@to_si ||= ->(x){x}
|
25
|
+
|
26
|
+
@derivation = Hash.new(0)
|
27
|
+
@derivation[@parent] += 1 unless @parent.nil?
|
69
28
|
|
70
29
|
self
|
71
30
|
end
|
@@ -109,11 +68,6 @@ class NumericWithUnit
|
|
109
68
|
raise unless [TrueClass, FalseClass].any?{|klass|arg.is_a?(klass)}
|
110
69
|
@si = arg
|
111
70
|
end
|
112
|
-
|
113
|
-
def derivation=(arg)
|
114
|
-
raise unless arg.is_a?(Hash)
|
115
|
-
@derivation = arg
|
116
|
-
end
|
117
71
|
end
|
118
72
|
end
|
119
73
|
end
|
@@ -156,11 +110,68 @@ class NumericWithUnit
|
|
156
110
|
}
|
157
111
|
|
158
112
|
# class methods
|
113
|
+
|
114
|
+
# create new unit from derivation _(for internal use)_ .
|
115
|
+
def self.derive
|
116
|
+
derivation = Hash.new(0)
|
117
|
+
yield(derivation)
|
118
|
+
|
119
|
+
return Unit.new if derivation.empty?
|
120
|
+
|
121
|
+
dimension = Hash.new(0)
|
122
|
+
|
123
|
+
h = derivation.sort_by{|u,v| u.symbol}.sort_by{|u,v| v} # ←どうしよう
|
124
|
+
|
125
|
+
s1 = h.select{|u,v| v > 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}.join('.')
|
126
|
+
s2 = h.select{|u,v| v < 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}.join('.')
|
127
|
+
symbol = s1 + (s2.empty? ? '' : "/(#{s2})")
|
128
|
+
|
129
|
+
derivation.each do |u,v|
|
130
|
+
u.dimension.each do |d,i|
|
131
|
+
dimension[d] += i*v
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
from_si = derivation.map{|u,v|
|
136
|
+
prc = if v > 0
|
137
|
+
->(x){u.from_si(x)}
|
138
|
+
else
|
139
|
+
->(x){x.quo(u.from_si(1)-u.from_si(0))} # ℃とKの変換のような場合に、変換式の切片を消すため。変換式が線形じゃないケースは想定していない
|
140
|
+
end
|
141
|
+
[prc, v.abs]
|
142
|
+
}.map{|prc,v|
|
143
|
+
->(x){ v.times{x = prc[x]}; x }
|
144
|
+
}.reduce{|memo, prc|
|
145
|
+
->(x){memo[prc[x]]}
|
146
|
+
}
|
147
|
+
|
148
|
+
to_si = derivation.map{|u,v|
|
149
|
+
prc = if v > 0
|
150
|
+
->(x){u.to_si(x)}
|
151
|
+
else
|
152
|
+
->(x){x.quo(u.to_si(1)-u.to_si(0))} # ℃とKの変換のような場合に、変換式の切片を消すため。変換式が線形じゃないケースは想定していない
|
153
|
+
end
|
154
|
+
[prc, v.abs]
|
155
|
+
}.map{|prc,v|
|
156
|
+
->(x){ v.times{x = prc[x]}; x }
|
157
|
+
}.reduce{|memo, prc|
|
158
|
+
->(x){memo[prc[x]]}
|
159
|
+
}
|
160
|
+
|
161
|
+
self.new{|conf|
|
162
|
+
conf.symbol = symbol
|
163
|
+
conf.dimension = dimension
|
164
|
+
conf.from_si = from_si
|
165
|
+
conf.to_si = to_si
|
166
|
+
}
|
167
|
+
end
|
159
168
|
|
169
|
+
# apply to_s to arg and return parsed unit.
|
160
170
|
def self.[](arg)
|
161
171
|
self.parse(arg.to_s)
|
162
172
|
end
|
163
173
|
|
174
|
+
# cast unit and add unit to base unit list at the same time.
|
164
175
|
def self.[]=(key, arg)
|
165
176
|
if arg.is_a?(Array) and arg.size == 2
|
166
177
|
a = [key, arg.first]
|
@@ -172,10 +183,12 @@ class NumericWithUnit
|
|
172
183
|
@@list << (u.is_a?(self) ? u : self[u]).cast(*a)
|
173
184
|
end
|
174
185
|
|
186
|
+
# return base unit list.
|
175
187
|
def self.list
|
176
188
|
@@list.map(&:symbol)
|
177
189
|
end
|
178
190
|
|
191
|
+
# add unit to base unit list.
|
179
192
|
def self.<<(arg)
|
180
193
|
if arg.is_a?(self)
|
181
194
|
@@list << arg
|
@@ -184,75 +197,125 @@ class NumericWithUnit
|
|
184
197
|
end
|
185
198
|
end
|
186
199
|
|
200
|
+
# remove unit from base unit list.
|
187
201
|
def self.delete(unit_symbol)
|
188
202
|
@@list.delete_if{|unit| unit.symbol == unit_symbol}
|
189
203
|
end
|
190
204
|
|
205
|
+
# create new unit and add unit to base unit list at the same time.
|
191
206
|
def self.assign
|
192
207
|
@@list << self.new{|config| yield(config)}
|
193
208
|
end
|
194
|
-
|
209
|
+
|
210
|
+
|
211
|
+
# parsing unit_str (ex. "kg", km/hr", "cm2") to (derived) unit.
|
195
212
|
def self.parse(unit_str)
|
196
|
-
|
197
|
-
|
213
|
+
rec = ->(arg){__send__(__method__, arg)}
|
214
|
+
|
215
|
+
dervation_str = parse_3rd(parse_2nd(parse_1st(unit_str)))
|
216
|
+
derive{|derivation|
|
217
|
+
dervation_str.each do |unit_str, order|
|
218
|
+
if i = @@list.rindex{|unit| unit.symbol == unit_str}
|
219
|
+
derivation[@@list[i]] += order
|
220
|
+
elsif m = unit_str.match(/^(?<prefix>#{@@prefix.keys.join('|')})(?<unit>#{list.join('|')})$/) and m[:unit].empty?.!
|
221
|
+
u = rec[m[:unit]].cast(unit_str, @@prefix[m[:prefix]])
|
222
|
+
derivation[u] += order
|
223
|
+
else
|
224
|
+
raise NoUnitError, "[#{unit_str}] is not defined!"
|
225
|
+
end
|
226
|
+
end
|
227
|
+
}
|
198
228
|
end
|
199
|
-
|
200
229
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
def self.parse_1st(unit_str)
|
205
|
-
i = @@list.rindex{|u| u.symbol == unit_str}
|
206
|
-
return @@list[i] if i
|
207
|
-
|
208
|
-
return unit_str if unit_str =~ /^[\.\/]$/
|
209
|
-
|
210
|
-
# 再帰で呼び出す用
|
230
|
+
def self.parse_1st(unit_str) #:nodoc:
|
231
|
+
return [unit_str] if @@list.rindex{|u| u.symbol == unit_str}
|
232
|
+
|
211
233
|
rec = ->(arg){__send__(__method__, arg)}
|
212
234
|
|
213
|
-
a =
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
235
|
+
a = []
|
236
|
+
tmp = ''
|
237
|
+
nest = 0
|
238
|
+
unit_str.each_char do |char|
|
239
|
+
nest -= 1 if char == ')'
|
240
|
+
|
241
|
+
if nest == 0
|
242
|
+
case char
|
243
|
+
when '(', ')'
|
244
|
+
a << rec[tmp] unless tmp.empty?
|
245
|
+
tmp = ''
|
246
|
+
when '.', '/'
|
247
|
+
a << tmp unless tmp.empty?
|
248
|
+
a << char
|
249
|
+
tmp = ''
|
250
|
+
else
|
251
|
+
tmp += char
|
252
|
+
end
|
253
|
+
else
|
254
|
+
tmp += char
|
255
|
+
end
|
256
|
+
|
257
|
+
nest += 1 if char == '('
|
258
|
+
raise StandardError, "parse error" if nest < 0
|
259
|
+
end
|
260
|
+
a << tmp unless tmp.empty?
|
261
|
+
a
|
224
262
|
end
|
225
|
-
private_class_method :parse_1st
|
226
263
|
|
227
|
-
|
228
|
-
|
229
|
-
def self.parse_2nd(unit_array)
|
230
|
-
# 再帰で呼び出す用
|
264
|
+
|
265
|
+
def self.parse_2nd(unit_array) #:nodoc:
|
231
266
|
rec = ->(arg){__send__(__method__, arg)}
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
unit_array.each do |
|
238
|
-
|
239
|
-
|
240
|
-
|
267
|
+
|
268
|
+
a = []
|
269
|
+
sign = 1
|
270
|
+
order = 1
|
271
|
+
tmp = nil
|
272
|
+
unit_array.each do |unit_x|
|
273
|
+
if tmp and not( unit_x.is_a?(String) and unit_x =~ /^\d+$/ )
|
274
|
+
a << {unit: tmp, order: sign*order} if tmp
|
275
|
+
sign = 1
|
276
|
+
order = 1
|
277
|
+
tmp = nil
|
278
|
+
end
|
279
|
+
|
280
|
+
case unit_x
|
241
281
|
when '.'
|
242
|
-
|
282
|
+
sign = 1
|
243
283
|
when '/'
|
244
|
-
|
245
|
-
when
|
246
|
-
|
247
|
-
when
|
248
|
-
|
284
|
+
sign = -1
|
285
|
+
when /^(-?\d+)$/
|
286
|
+
order = $1.to_i
|
287
|
+
when /^(.+?)(-?\d+)$/
|
288
|
+
order = $2.to_i
|
289
|
+
tmp = $1
|
290
|
+
else
|
291
|
+
tmp = unit_x.is_a?(Array) ? rec[unit_x] : unit_x
|
249
292
|
end
|
250
293
|
end
|
251
|
-
|
252
|
-
|
294
|
+
a << {unit: tmp, order: sign*order} if tmp
|
295
|
+
a
|
253
296
|
end
|
254
|
-
|
255
|
-
|
297
|
+
|
298
|
+
def self.parse_3rd(unit_x, derivation=Hash.new(0), order=1) #:nodoc:
|
299
|
+
rec = ->(*arg){__send__(__method__, *arg)}
|
300
|
+
|
301
|
+
if unit_x.is_a?(Hash)
|
302
|
+
if unit_x[:unit].is_a?(Array)
|
303
|
+
rec[unit_x[:unit], derivation, order * unit_x[:order]]
|
304
|
+
else
|
305
|
+
derivation[unit_x[:unit]] += (order * unit_x[:order])
|
306
|
+
end
|
307
|
+
elsif unit_x.is_a?(Array)
|
308
|
+
unit_x.each do |x|
|
309
|
+
rec[x, derivation, order]
|
310
|
+
end
|
311
|
+
else
|
312
|
+
raise StandardError, "maybe bug"
|
313
|
+
end
|
314
|
+
derivation
|
315
|
+
end
|
316
|
+
|
317
|
+
private_class_method :parse_1st, :parse_2nd, :parse_3rd
|
318
|
+
|
256
319
|
end
|
257
320
|
|
258
321
|
|
@@ -261,12 +324,10 @@ class NumericWithUnit
|
|
261
324
|
|
262
325
|
# Instance Methods
|
263
326
|
|
264
|
-
# attr_accessor :symbol
|
265
327
|
attr_reader :symbol
|
266
328
|
attr_reader :dimension, :derivation
|
267
329
|
|
268
330
|
def initialize
|
269
|
-
|
270
331
|
# Unit::Configとinitializeの役割が分離できていないので見なおせ
|
271
332
|
config = Config.new(self)
|
272
333
|
yield(config) if block_given?
|
@@ -280,6 +341,8 @@ class NumericWithUnit
|
|
280
341
|
@derivation = config.derivation
|
281
342
|
end
|
282
343
|
|
344
|
+
# create new unit with new symbol and factor from self.
|
345
|
+
# use for converting [in] = 25.4[mm] .
|
283
346
|
def cast(new_symbol, factor = 1)
|
284
347
|
self.class.new do |conf|
|
285
348
|
conf.symbol = new_symbol
|
@@ -306,6 +369,7 @@ class NumericWithUnit
|
|
306
369
|
@dimension.all?{|k,v| v.zero?}
|
307
370
|
end
|
308
371
|
|
372
|
+
# return true if self and other_unit have the same dimension.
|
309
373
|
def dimension_equal?(other_unit)
|
310
374
|
(@dimension.keys | other_unit.dimension.keys).all?{|k|
|
311
375
|
@dimension[k] == other_unit.dimension[k]
|
@@ -321,16 +385,16 @@ class NumericWithUnit
|
|
321
385
|
end
|
322
386
|
|
323
387
|
def *(other_unit)
|
324
|
-
self.class.
|
325
|
-
@derivation.each{|k, v|
|
326
|
-
other_unit.derivation.each{|k, v|
|
388
|
+
self.class.derive do |derivation|
|
389
|
+
@derivation.each{|k, v| derivation[k] += v}
|
390
|
+
other_unit.derivation.each{|k, v| derivation[k] += v}
|
327
391
|
end
|
328
392
|
end
|
329
393
|
|
330
394
|
def /(other_unit)
|
331
|
-
self.class.
|
332
|
-
@derivation.each{|k, v|
|
333
|
-
other_unit.derivation.each{|k, v|
|
395
|
+
self.class.derive do |derivation|
|
396
|
+
@derivation.each{|k, v| derivation[k] += v}
|
397
|
+
other_unit.derivation.each{|k, v| derivation[k] -= v}
|
334
398
|
end
|
335
399
|
end
|
336
400
|
|
@@ -338,10 +402,10 @@ class NumericWithUnit
|
|
338
402
|
if num.zero?
|
339
403
|
self.class.new
|
340
404
|
else
|
341
|
-
self.class.
|
405
|
+
self.class.derive do |derivation|
|
342
406
|
# ここto_iでOKか?v*numが整数じゃなければraiseすべき?→すべき→NumericWithUnitでやるべき?
|
343
407
|
# Unitでは整数じゃない次数の単位は許容すべきか否か→していい気がする
|
344
|
-
@derivation.each{|k, v|
|
408
|
+
@derivation.each{|k, v| derivation[k] = (v*num).to_i}
|
345
409
|
end
|
346
410
|
end
|
347
411
|
end
|
@@ -107,7 +107,6 @@ class NumericWithUnit
|
|
107
107
|
|
108
108
|
# Electriccal Resistance
|
109
109
|
Unit['Ω'] = 'V/A'
|
110
|
-
Unit['ohm'] = 'Ω'
|
111
110
|
|
112
111
|
# Electriccal Conductance
|
113
112
|
Unit['S'] = 'A/V'
|
@@ -131,7 +130,6 @@ class NumericWithUnit
|
|
131
130
|
conf.from_si{|x| k.from_si(x)-intercept}
|
132
131
|
conf.to_si{|x| k.to_si(x+intercept)}
|
133
132
|
end
|
134
|
-
Unit['degC'] = '℃'
|
135
133
|
|
136
134
|
# Luminouse flux
|
137
135
|
Unit['lx'] = 'cd.sr'
|
@@ -4,8 +4,7 @@
|
|
4
4
|
# 独断と偏見による一般的な単位
|
5
5
|
#
|
6
6
|
|
7
|
-
require 'numeric_with_unit/
|
8
|
-
require 'numeric_with_unit/base_unit'
|
7
|
+
require 'numeric_with_unit/unit_definition/base'
|
9
8
|
|
10
9
|
class NumericWithUnit
|
11
10
|
# Dimensionless
|
@@ -22,6 +21,9 @@ class NumericWithUnit
|
|
22
21
|
Unit['ton'] = 1000, 'kg'
|
23
22
|
Unit['oz'] = "28.349523125".to_r, 'g'
|
24
23
|
Unit['lb'] = 16, 'oz'
|
24
|
+
|
25
|
+
# Electriccal Resistance
|
26
|
+
Unit['ohm'] = 'Ω'
|
25
27
|
|
26
28
|
# Temperature
|
27
29
|
Unit['degC'] = '℃'
|
@@ -42,6 +44,7 @@ class NumericWithUnit
|
|
42
44
|
Unit['ft'] = "1/3".to_r, 'yd'
|
43
45
|
Unit['in'] = "1/12".to_r, 'ft'
|
44
46
|
Unit['mi'] = 5280, 'ft'
|
47
|
+
Unit['100m'] = 100, 'm' # [kPa/100m]とか
|
45
48
|
|
46
49
|
# Volume
|
47
50
|
Unit['cc'] = 'cm3'
|
@@ -6,9 +6,9 @@
|
|
6
6
|
require 'numeric_with_unit'
|
7
7
|
|
8
8
|
class NumericWithUnit
|
9
|
-
def method_missing(*args)
|
10
|
-
if args.
|
11
|
-
unit_str =
|
9
|
+
def method_missing(name, *args)
|
10
|
+
if args.empty?
|
11
|
+
unit_str = name.to_s.gsub('_', '/')
|
12
12
|
unit_chain_util(Unit[unit_str])
|
13
13
|
else
|
14
14
|
raise Unit::NoUnitError
|
@@ -39,9 +39,9 @@ end
|
|
39
39
|
|
40
40
|
class NumericWithUnit
|
41
41
|
module NumUtil
|
42
|
-
def method_missing(*args)
|
43
|
-
if args.
|
44
|
-
unit_str =
|
42
|
+
def method_missing(name, *args)
|
43
|
+
if args.empty?
|
44
|
+
unit_str = name.to_s.gsub('_', '/')
|
45
45
|
self.rationalize.to_nwu(unit_str) # util2は利便性優先なのでratoinalizeしてしまいます
|
46
46
|
else
|
47
47
|
raise Unit::NoUnitError
|
data/lib/numeric_with_unit.rb
CHANGED
@@ -12,33 +12,40 @@ class NumericWithUnit
|
|
12
12
|
@unit = unit.is_a?(Unit) ? unit : Unit[unit]
|
13
13
|
end
|
14
14
|
|
15
|
+
# Return String for inspect
|
15
16
|
def inspect
|
16
17
|
"#{@value.inspect} [#{@unit.symbol}] #{unit.dimension.inspect}"
|
17
18
|
end
|
18
19
|
|
20
|
+
# Return String with value and unit symbol
|
19
21
|
def to_s
|
20
22
|
"#{@value.to_s} #{@unit.symbol}"
|
21
23
|
end
|
22
24
|
|
23
|
-
#
|
25
|
+
# If ohter is NumericWithUnit and same dimension, comparing value with converting to si.
|
26
|
+
# Else return nil.
|
24
27
|
def <=>(other)
|
25
28
|
if other.is_a?(self.class) and @unit.dimension_equal? other.unit
|
26
29
|
@unit.to_si(@value) <=> other.unit.to_si(other.value)
|
27
30
|
end
|
28
31
|
end
|
29
32
|
|
33
|
+
# Return succed value with same unit.
|
30
34
|
def succ
|
31
35
|
self.class.new(@value.succ, @unit)
|
32
36
|
end
|
33
37
|
|
38
|
+
# Return value.to_i
|
34
39
|
def to_i
|
35
40
|
@value.to_i
|
36
41
|
end
|
37
42
|
|
43
|
+
# Return value.to_f
|
38
44
|
def to_f
|
39
45
|
@value.to_f
|
40
46
|
end
|
41
47
|
|
48
|
+
# Return NumericWithUnit with given unit
|
42
49
|
def to_nwu(unit)
|
43
50
|
new_unit = unit.is_a?(Unit) ? unit : Unit[unit]
|
44
51
|
|
@@ -181,13 +188,13 @@ end
|
|
181
188
|
class String
|
182
189
|
def to_nwu(mthd=:to_r)
|
183
190
|
m = self.match /(?<value>.+) (?<unit>.+)/ # 適当
|
184
|
-
NumericWithUnit
|
191
|
+
NumericWithUnit.new(m[:value].__send__(mthd), m[:unit])
|
185
192
|
end
|
186
193
|
end
|
187
194
|
|
188
195
|
|
189
196
|
|
190
197
|
# unit definition
|
191
|
-
require 'numeric_with_unit/
|
192
|
-
require 'numeric_with_unit/
|
198
|
+
require 'numeric_with_unit/unit_definition/base'
|
199
|
+
require 'numeric_with_unit/unit_definition/common'
|
193
200
|
|
metadata
CHANGED
@@ -1,28 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: numeric_with_unit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- diaphragm
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: This gem provide NumericWithUnit class to calculate numeric with unit
|
14
|
+
of measurement.
|
14
15
|
email:
|
15
16
|
executables: []
|
16
17
|
extensions: []
|
17
18
|
extra_rdoc_files: []
|
18
19
|
files:
|
19
20
|
- lib/numeric_with_unit.rb
|
20
|
-
- lib/numeric_with_unit/base_unit.rb
|
21
|
-
- lib/numeric_with_unit/cgs_unit.rb
|
22
|
-
- lib/numeric_with_unit/common_unit.rb
|
23
|
-
- lib/numeric_with_unit/imperial_unit.rb
|
24
|
-
- lib/numeric_with_unit/natural_unit.rb
|
25
21
|
- lib/numeric_with_unit/unit.rb
|
22
|
+
- lib/numeric_with_unit/unit_definition/base.rb
|
23
|
+
- lib/numeric_with_unit/unit_definition/cgs.rb
|
24
|
+
- lib/numeric_with_unit/unit_definition/common.rb
|
25
|
+
- lib/numeric_with_unit/unit_definition/imperial.rb
|
26
|
+
- lib/numeric_with_unit/unit_definition/natural.rb
|
26
27
|
- lib/numeric_with_unit/util.rb
|
27
28
|
- lib/numeric_with_unit/util2.rb
|
28
29
|
homepage: https://github.com/diaphragm/ruby-numeric-with-unit
|
@@ -37,7 +38,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
38
|
requirements:
|
38
39
|
- - ">="
|
39
40
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
41
|
+
version: 2.0.0
|
41
42
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
43
|
requirements:
|
43
44
|
- - ">="
|
@@ -45,8 +46,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
46
|
version: '0'
|
46
47
|
requirements: []
|
47
48
|
rubyforge_project:
|
48
|
-
rubygems_version: 2.
|
49
|
+
rubygems_version: 2.4.5
|
49
50
|
signing_key:
|
50
51
|
specification_version: 4
|
51
|
-
summary:
|
52
|
+
summary: Numerical calculation with unit of measurement
|
52
53
|
test_files: []
|