minad-units 0.1.2 → 0.1.3
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.
- data/lib/systems/degree.yml +2 -3
- data/lib/systems/misc.yml +4 -0
- data/lib/systems/si.yml +2 -2
- data/lib/systems/time.yml +2 -0
- data/lib/units.rb +32 -13
- data/spec/spec_units.rb +1 -0
- metadata +1 -1
data/lib/systems/degree.yml
CHANGED
data/lib/systems/misc.yml
CHANGED
data/lib/systems/si.yml
CHANGED
|
@@ -25,7 +25,7 @@ units:
|
|
|
25
25
|
|
|
26
26
|
# Derived SI units
|
|
27
27
|
radiant:
|
|
28
|
-
sym: rad
|
|
28
|
+
sym: [rad, radian]
|
|
29
29
|
def: 1
|
|
30
30
|
steradiant:
|
|
31
31
|
sym: sr
|
|
@@ -70,7 +70,7 @@ units:
|
|
|
70
70
|
sym: H
|
|
71
71
|
def: weber / ampere
|
|
72
72
|
celsius:
|
|
73
|
-
sym: °C
|
|
73
|
+
sym: [°C, ℃]
|
|
74
74
|
def: kelvin
|
|
75
75
|
lumen:
|
|
76
76
|
sym: lm
|
data/lib/systems/time.yml
CHANGED
data/lib/units.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
require 'yaml'
|
|
3
3
|
|
|
4
4
|
class Unit < Numeric
|
|
5
|
-
VERSION = '0.1.
|
|
5
|
+
VERSION = '0.1.3'
|
|
6
6
|
|
|
7
7
|
attr_reader :numerator, :denominator, :unit, :normalized, :system
|
|
8
8
|
|
|
@@ -102,7 +102,7 @@ class Unit < Numeric
|
|
|
102
102
|
def compatible?(other)
|
|
103
103
|
a, b = coerce(other)
|
|
104
104
|
a, b = a.normalize, b.normalize
|
|
105
|
-
a.unit == b.unit
|
|
105
|
+
(a.unit == b.unit) || (a == 0 || b == 0)
|
|
106
106
|
end
|
|
107
107
|
|
|
108
108
|
alias compatible_with? compatible?
|
|
@@ -123,14 +123,26 @@ class Unit < Numeric
|
|
|
123
123
|
s << "/#{@denominator}" if @denominator != 1
|
|
124
124
|
positive = @unit.select {|prefix, name, exp| exp >= 0 }
|
|
125
125
|
negative = @unit.select {|prefix, name, exp| exp < 0 }
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
s << '
|
|
126
|
+
s << ' ' << unit_string(positive, '·') if !positive.empty?
|
|
127
|
+
if !negative.empty?
|
|
128
|
+
s << ' 1' if positive.empty?
|
|
129
|
+
s << '/' << unit_string(negative, '·')
|
|
130
130
|
end
|
|
131
|
+
s
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def to_tex
|
|
135
|
+
s = '\SI{'
|
|
136
|
+
s << @numerator.to_s
|
|
137
|
+
s << "/#{@denominator}" if @denominator != 1
|
|
138
|
+
positive = @unit.select {|prefix, name, exp| exp >= 0 }
|
|
139
|
+
negative = @unit.select {|prefix, name, exp| exp < 0 }
|
|
140
|
+
s << '}{' << unit_string(positive, '.') if !positive.empty?
|
|
131
141
|
if !negative.empty?
|
|
132
|
-
s << '
|
|
142
|
+
s << ' 1' if positive.empty?
|
|
143
|
+
s << '/' << unit_string(negative, '.')
|
|
133
144
|
end
|
|
145
|
+
s << '}'
|
|
134
146
|
s
|
|
135
147
|
end
|
|
136
148
|
|
|
@@ -146,7 +158,8 @@ class Unit < Numeric
|
|
|
146
158
|
to_f.unit(unit)
|
|
147
159
|
end
|
|
148
160
|
|
|
149
|
-
def to_unit(system)
|
|
161
|
+
def to_unit(system = nil)
|
|
162
|
+
system ||= Unit::System::DEFAULT
|
|
150
163
|
raise TypeError, 'Different unit system' if @system != system
|
|
151
164
|
self
|
|
152
165
|
end
|
|
@@ -170,7 +183,7 @@ class Unit < Numeric
|
|
|
170
183
|
|
|
171
184
|
private
|
|
172
185
|
|
|
173
|
-
def unit_string(list)
|
|
186
|
+
def unit_string(list, sep)
|
|
174
187
|
units = []
|
|
175
188
|
list.each do |prefix, name, exp|
|
|
176
189
|
unit = ''
|
|
@@ -179,7 +192,7 @@ class Unit < Numeric
|
|
|
179
192
|
unit << '^' << exp.abs.to_s if exp.abs != 1
|
|
180
193
|
units << unit
|
|
181
194
|
end
|
|
182
|
-
units.sort.join(
|
|
195
|
+
units.sort.join(sep)
|
|
183
196
|
end
|
|
184
197
|
|
|
185
198
|
def self.power_unit(unit, pow)
|
|
@@ -351,9 +364,9 @@ class Unit < Numeric
|
|
|
351
364
|
|
|
352
365
|
REAL = /^-?(?:(?:\d*\.\d+|\d+\.\d*)(?:[eE][-+]?\d+)?|\d+[eE][-+]?\d+)$/
|
|
353
366
|
DEC = /^-?\d+$/
|
|
354
|
-
SYMBOL = /^[a-zA-Z_][\w_]*$/
|
|
355
|
-
OPERATOR = { '/' => ['/', 1], '*' => ['*', 1], '·' => ['*', 1], '^' => ['^', 2] }
|
|
356
|
-
OPERATOR_TOKENS = OPERATOR.keys.map {|x| Regexp.quote(x) }
|
|
367
|
+
SYMBOL = /^[a-zA-Z_°'"][\w_°'"]*$/
|
|
368
|
+
OPERATOR = { '/' => ['/', 1], '*' => ['*', 1], '·' => ['*', 1], '^' => ['^', 2], '**' => ['^', 2] }
|
|
369
|
+
OPERATOR_TOKENS = OPERATOR.keys.sort_by {|x| -x.size }. map {|x| Regexp.quote(x) }
|
|
357
370
|
VALUE_TOKENS = [REAL.source[1..-2], DEC.source[1..-2], SYMBOL.source[1..-2]]
|
|
358
371
|
TOKENIZER = Regexp.new((OPERATOR_TOKENS + VALUE_TOKENS + ['\\(', '\\)']).join('|'))
|
|
359
372
|
|
|
@@ -460,6 +473,12 @@ class String
|
|
|
460
473
|
end
|
|
461
474
|
end
|
|
462
475
|
|
|
476
|
+
class Symbol
|
|
477
|
+
def to_unit(system = nil)
|
|
478
|
+
to_s.to_unit(system)
|
|
479
|
+
end
|
|
480
|
+
end
|
|
481
|
+
|
|
463
482
|
class Array
|
|
464
483
|
def to_unit(system = nil)
|
|
465
484
|
system ||= Unit::System::DEFAULT
|
data/spec/spec_units.rb
CHANGED
|
@@ -68,6 +68,7 @@ describe 'Unit' do
|
|
|
68
68
|
1.MeV.in_joule.should.equal Unit(1.602176487e-13, 'joule')
|
|
69
69
|
1.kilometer.in_meter.should.equal Unit(1000, 'meter')
|
|
70
70
|
1.liter.in('meter^3').should.equal Unit(1, 1000, 'meter^3')
|
|
71
|
+
1.kilometer_per_hour.in_meter_per_second.should.equal Unit(5, 18, 'meter/second')
|
|
71
72
|
end
|
|
72
73
|
|
|
73
74
|
it 'should have a working compatible? method' do
|