units-system 0.2.0 → 0.2.1
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/VERSION +1 -1
- data/lib/units/measure.rb +10 -1
- data/test/test_units-system.rb +8 -0
- data/units-system.gemspec +1 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/units/measure.rb
CHANGED
@@ -65,8 +65,17 @@ module Units
|
|
65
65
|
end
|
66
66
|
|
67
67
|
# more natural concise text representation
|
68
|
+
# If a block is passed, it is used to format the numeric magnitudes (Float numbers) (e.g., for localization)
|
69
|
+
# Units.units{3*m/(2*s)}.abr{|v| v.to_s.tr('.',',') } # => "1,5 m/s"
|
68
70
|
def abr
|
69
|
-
self.to_s.gsub('**','^').tr('*',' ')
|
71
|
+
txt = self.to_s.gsub('**','^').tr('*',' ')
|
72
|
+
if block_given?
|
73
|
+
txt.gsub!(/[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)([eE][+-]?(\d+_?)*\d+)?/) do
|
74
|
+
v = $&.to_f
|
75
|
+
yield v
|
76
|
+
end
|
77
|
+
end
|
78
|
+
txt
|
70
79
|
end
|
71
80
|
|
72
81
|
# decompose compound units
|
data/test/test_units-system.rb
CHANGED
@@ -142,4 +142,12 @@ class TestUnitsSystem < Test::Unit::TestCase
|
|
142
142
|
assert_equal Units.u{m-km}, Units.u{m}-'km'
|
143
143
|
end
|
144
144
|
|
145
|
+
should "represent abbreviated units" do
|
146
|
+
assert_equal "1.0 m", Units.u{m}.abr
|
147
|
+
assert_equal "3.0 m", Units.u{3*m}.abr
|
148
|
+
assert_equal "3.0 m/s", Units.u{3*m/s}.abr
|
149
|
+
assert_equal "3.5555555555555554 m/s", Units.u{3*m/s + 2*km/h}.abr
|
150
|
+
assert_equal "1,5 m/s", Units.units{3*m/(2*s)}.abr{|v| v.to_s.tr('.',',') }
|
151
|
+
end
|
152
|
+
|
145
153
|
end
|
data/units-system.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: units-system
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -129,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
129
|
version: '0'
|
130
130
|
segments:
|
131
131
|
- 0
|
132
|
-
hash:
|
132
|
+
hash: -3773164619395754459
|
133
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
134
|
none: false
|
135
135
|
requirements:
|