ruby-units 1.4.5 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.txt +45 -44
- data/README.md +190 -129
- data/Rakefile.rb +13 -77
- data/VERSION +1 -1
- data/lib/ruby-units.rb +0 -6
- data/lib/ruby_units/array.rb +1 -3
- data/lib/ruby_units/date.rb +30 -32
- data/lib/ruby_units/definition.rb +13 -13
- data/lib/ruby_units/namespaced.rb +0 -1
- data/lib/ruby_units/numeric.rb +0 -2
- data/lib/ruby_units/string.rb +11 -18
- data/lib/ruby_units/time.rb +10 -12
- data/lib/ruby_units/unit.rb +102 -137
- data/lib/ruby_units/unit_definitions/prefix.rb +30 -30
- data/lib/ruby_units/unit_definitions/standard.rb +11 -1
- data/ruby-units.gemspec +9 -14
- metadata +6 -29
- data/TODO +0 -3
- data/lib/ruby_units.rb +0 -9
- data/lib/ruby_units/fixnum.rb +0 -22
- data/lib/ruby_units/object.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 28d6495ddf8e123add28cd254bcf502cdbe5cba0
|
4
|
+
data.tar.gz: f5304368e066e2005fa686fefd2818c264684d4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80b4d5e20703fdb745480e85de19088a3aa610eb16bb9e753365530aa325363dc3f2287df227e0fd4b33703334d0df3868039098fc8972736e88f29918496db0
|
7
|
+
data.tar.gz: 4ba7e2f4964ce834ec6d4c585a8be96ce5b68069c0ffa75e7cae0bf1996236c0fdd928372d92e9b8621c4c4d821d3d32f8cf0e57c9fb40d954dd13e8ee99e279
|
data/CHANGELOG.txt
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
Change Log for Ruby-units
|
2
2
|
=========================
|
3
|
-
|
3
|
+
2015-11-07 2.0.0 * remove support for ruby versions less than 2.0
|
4
|
+
* remove `.unit` and `.u` from String
|
5
|
+
* remove `.unit` from Date
|
6
|
+
* Fix an issue with redefining units that have already been cached
|
7
|
+
* remove 'U()' and 'u()' constructors
|
8
|
+
* Fix issue #123 -- Fixes for singular unit parsing
|
9
|
+
2015-07-16 * Fix issue #129 -- doesn't handle number in the denominator
|
10
|
+
2015-05-09 * update test harness to use rspec 3
|
11
|
+
2014-02-21 1.4.5 * Fix issue #98 -- add mcg as a valid unit
|
4
12
|
2013-07-19 1.4.4 * Fix issue #4 -- .best_prefix method
|
5
13
|
* Fix issue #60 -- Consider placing Unit in a module
|
6
14
|
* Fix issue #75 -- Siemens is kind of conductance not resistance
|
@@ -25,7 +33,7 @@ Change Log for Ruby-units
|
|
25
33
|
2012-01-02 1.4.0 * Fix some definitions that were just wrong (amu, dalton)
|
26
34
|
* Definition uses name of unit if no aliases provided
|
27
35
|
* Refactor definition process. New units are immediately available
|
28
|
-
2011-12-31 * Define standard units in terms of base and other standard units -- more internally consistent
|
36
|
+
2011-12-31 * Define standard units in terms of base and other standard units -- more internally consistent
|
29
37
|
and less prone to round-off errors.
|
30
38
|
* add 'poundal'
|
31
39
|
* remove 'wtpercent'
|
@@ -33,7 +41,7 @@ Change Log for Ruby-units
|
|
33
41
|
* Define compound units with base units for consistency
|
34
42
|
* distinguish between a league and a nautical league
|
35
43
|
* NOTE: the new unit definition DSL is not backwardly compatible with the old method
|
36
|
-
(which is now deprecated).
|
44
|
+
(which is now deprecated).
|
37
45
|
* Fix issue #27
|
38
46
|
2011-12-18 * Can define a display_name for units (fixes #26)
|
39
47
|
2011-12-04 * Documentation improvements
|
@@ -61,17 +69,17 @@ Change Log for Ruby-units
|
|
61
69
|
2007-12-13 1.1.3 * fixed a minor bug with string %
|
62
70
|
2007-12-12 1.1.2 * fixed a bug with format strings
|
63
71
|
* detect if ruby 1.8.6 is installed and use its' to_date function
|
64
|
-
|
72
|
+
|
65
73
|
2007-07-14 1.1.1 * fixed bug that would prevent creating '<pound-mass>' units, which
|
66
74
|
prevented rounding from working
|
67
75
|
* tests do not fail if Uncertain gem is not installed, you just get an
|
68
76
|
annoying warning message
|
69
|
-
|
77
|
+
|
70
78
|
2007-01-28 1.1.0 * completely revamped the temperature handling system (see README)
|
71
79
|
* fixed some spelling errors in some units
|
72
80
|
* fixed to_datetime and to_date to convert durations to datetimes and dates'
|
73
81
|
|
74
|
-
2007-01-24 1.0.2 * Minor changes in the way powers are calculated to support Uncertain
|
82
|
+
2007-01-24 1.0.2 * Minor changes in the way powers are calculated to support Uncertain
|
75
83
|
numbers better.
|
76
84
|
* Fixed parsing bug with Uncertain Numbers
|
77
85
|
* added resolution / typography units (pixels, points, pica)
|
@@ -87,17 +95,17 @@ Change Log for Ruby-units
|
|
87
95
|
'1+1i mm'.unit to get a complex unit.
|
88
96
|
* Taking the root of a negative unit will give you a complex unit
|
89
97
|
* fixed unary minus to work again
|
90
|
-
* Math.hypot now takes units. Both parameters must be the compatible
|
91
|
-
units or it will assert. Units will be converted to a common base
|
98
|
+
* Math.hypot now takes units. Both parameters must be the compatible
|
99
|
+
units or it will assert. Units will be converted to a common base
|
92
100
|
before use.
|
93
|
-
* Can now specify units in rational numbers, i.e., '1/4 cup'.unit
|
101
|
+
* Can now specify units in rational numbers, i.e., '1/4 cup'.unit
|
94
102
|
* Seems like a good time to move to 1.0 status
|
95
|
-
|
103
|
+
|
96
104
|
2006-12-15 0.3.9 * forgot to increment the version in the gem file..ooops.
|
97
105
|
|
98
|
-
2006-12-15 0.3.8 * Any object that supports a 'to_unit' method will now be
|
106
|
+
2006-12-15 0.3.8 * Any object that supports a 'to_unit' method will now be
|
99
107
|
automatically coerced to a unit during math operations.
|
100
|
-
|
108
|
+
|
101
109
|
2006-12-14 0.3.7 * improved handling of percents and added a 'wt%' unit
|
102
110
|
equivalent to 1 g/dl.
|
103
111
|
* Improved handling for units with non-alphanumeric names
|
@@ -111,21 +119,21 @@ Change Log for Ruby-units
|
|
111
119
|
* to_int now coerces the result to an actual Integer,
|
112
120
|
but only works properly for unitless Units.
|
113
121
|
|
114
|
-
2006-10-27 0.3.4 * Fixed a few more parsing bugs so that it will properly
|
122
|
+
2006-10-27 0.3.4 * Fixed a few more parsing bugs so that it will properly
|
115
123
|
complain about malformed units.
|
116
124
|
* Fixed a bug that prevents proper use of percents
|
117
|
-
* several minor tweaks
|
125
|
+
* several minor tweaks
|
118
126
|
* some improved Date and DateTime handling
|
119
127
|
* can convert between Date, DateTime, and Time objects
|
120
128
|
* Time math will now return a DateTime if it goes out of
|
121
129
|
range.
|
122
130
|
|
123
|
-
2006-10-03 0.3.3 * Apparently I can't do math late at night.
|
131
|
+
2006-10-03 0.3.3 * Apparently I can't do math late at night.
|
124
132
|
Fixed a bug that would cause problems when adding
|
125
|
-
or subtracting units to a unit with a zero scalar.
|
133
|
+
or subtracting units to a unit with a zero scalar.
|
126
134
|
* Date and DateTime objects can be converted to 'units'
|
127
135
|
|
128
|
-
2006-10-03 0.3.2 * More minor bug fixes
|
136
|
+
2006-10-03 0.3.2 * More minor bug fixes
|
129
137
|
(now fixes a minor name collision with rails)
|
130
138
|
|
131
139
|
2006-10-02 0.3.1 * minor bug fixes
|
@@ -133,10 +141,10 @@ Change Log for Ruby-units
|
|
133
141
|
2006-10-02 0.3.0 * Performance enhanced by caching results of many
|
134
142
|
functions (Thanks to Kurt Stephens for pushing this.)
|
135
143
|
* Throws an exception if the unit is not recognized
|
136
|
-
* units can now identify what 'kind' they are
|
144
|
+
* units can now identify what 'kind' they are
|
137
145
|
(:length, :mass, etc..)
|
138
|
-
* New constructors:
|
139
|
-
Unit(1,"mm")
|
146
|
+
* New constructors:
|
147
|
+
Unit(1,"mm")
|
140
148
|
Unit(1,"mm/s")
|
141
149
|
Unit(1,"mm","s")
|
142
150
|
|
@@ -149,8 +157,8 @@ Change Log for Ruby-units
|
|
149
157
|
* 'string'.time returns a Time object or a DateTime if the
|
150
158
|
Time object fails
|
151
159
|
* 'string'.datetime returns a DateTime or a Time if the
|
152
|
-
DateTime fails
|
153
|
-
|
160
|
+
DateTime fails
|
161
|
+
|
154
162
|
2006-09-19 0.2.2 * tweaked temperature handling a bit. Now enter
|
155
163
|
temperatures like this:
|
156
164
|
'0 tempC'.unit #=> 273.15 degK
|
@@ -158,7 +166,7 @@ Change Log for Ruby-units
|
|
158
166
|
problems when temperatures are used in equations.
|
159
167
|
* added Time.in("5 min")
|
160
168
|
* added Unit.to_unit to simplify some calls
|
161
|
-
|
169
|
+
|
162
170
|
2006-09-18 0.2.1 * Trig math functions (sin, cos, tan, sinh, cosh, tanh)
|
163
171
|
accept units that can be converted to radians
|
164
172
|
Math.sin("90 deg".unit) => 1.0
|
@@ -170,14 +178,14 @@ Change Log for Ruby-units
|
|
170
178
|
Time.now + "1 hr".unit => Mon Sep 18 11:51:29 EDT 2006
|
171
179
|
* can output time in 'hh:mm:ss' format by using
|
172
180
|
'unit.to_s(:time)'
|
173
|
-
* added time helper methods
|
174
|
-
ago,
|
175
|
-
since(Time/DateTime),
|
176
|
-
until(Time/DateTime),
|
177
|
-
from(Time/DateTime),
|
181
|
+
* added time helper methods
|
182
|
+
ago,
|
183
|
+
since(Time/DateTime),
|
184
|
+
until(Time/DateTime),
|
185
|
+
from(Time/DateTime),
|
178
186
|
before(Time/DateTime), and
|
179
187
|
after(Time/DateTime)
|
180
|
-
* Time helpers also work on strings. In this case they
|
188
|
+
* Time helpers also work on strings. In this case they
|
181
189
|
are first converted to units
|
182
190
|
'5 min'.from_now
|
183
191
|
'1 week'.ago
|
@@ -191,19 +199,19 @@ Change Log for Ruby-units
|
|
191
199
|
output
|
192
200
|
* can use U'1 mm' or '1 mm'.u to specify units now
|
193
201
|
|
194
|
-
2006-09-17 * can now use the '%' format specifier like
|
202
|
+
2006-09-17 * can now use the '%' format specifier like
|
195
203
|
'%0.2f' % '1 mm'.unit #=> '1.00 mm'
|
196
204
|
* works nicely with time now.
|
197
205
|
'1 week'.unit + Time.now => 1.159e+09 s
|
198
|
-
Time.at('1.159e+09 s'.unit)
|
206
|
+
Time.at('1.159e+09 s'.unit)
|
199
207
|
=> Sat Sep 23 04:26:40 EDT 2006
|
200
|
-
"1.159e9 s".unit.time
|
208
|
+
"1.159e9 s".unit.time
|
201
209
|
=> Sat Sep 23 04:26:40 EDT 2006
|
202
210
|
* Time.now.unit => 1.159e9 s
|
203
211
|
* works well with 'Uncertain' numerics
|
204
212
|
(www.rubyforge.org/projects/uncertain)
|
205
213
|
* Improved parsing
|
206
|
-
|
214
|
+
|
207
215
|
2006-08-28 0.2.0 * Added 'ruby_unit.rb' file so that requires will still
|
208
216
|
work if the wrong name is used
|
209
217
|
* Added 'to' as an alias to '>>' so conversions can be
|
@@ -226,13 +234,13 @@ Change Log for Ruby-units
|
|
226
234
|
from forcing the conversion. To get the scalar, just
|
227
235
|
use 'unit.scalar'
|
228
236
|
* 'inspect' returns string representation
|
229
|
-
* better edge-case detection with math functions.
|
237
|
+
* better edge-case detection with math functions.
|
230
238
|
"0 mm".unit**-1 now throws a ZeroDivisionError exception
|
231
|
-
* Ranges can make a series of units, so long as the end
|
239
|
+
* Ranges can make a series of units, so long as the end
|
232
240
|
points have integer scalars.
|
233
241
|
* Fixed a parsing bug with feet/pounds and scientific
|
234
242
|
numbers
|
235
|
-
|
243
|
+
|
236
244
|
2006-08-22 0.1.1 * Added new format option "1 mm".to_unit("in") now
|
237
245
|
converts the result to the indicated units
|
238
246
|
* Fixed some naming issues so that the gem name matches
|
@@ -249,12 +257,5 @@ Change Log for Ruby-units
|
|
249
257
|
[1,'mm','s'].unit === "1 mm/s".unit
|
250
258
|
2.5.unit === "2.5".unit
|
251
259
|
* Added instructions on how to add custom units
|
252
|
-
|
253
|
-
2006-08-22 0.1.0 * Initial Release
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
260
|
|
261
|
+
2006-08-22 0.1.0 * Initial Release
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
|
1
|
+
Ruby Units
|
2
|
+
==========
|
2
3
|
|
3
4
|
[![Build Status](https://secure.travis-ci.org/olbrich/ruby-units.png)](http://travis-ci.org/olbrich/ruby-units)
|
4
5
|
|
@@ -6,147 +7,202 @@ Kevin C. Olbrich, Ph.D.
|
|
6
7
|
|
7
8
|
Project page: [http://github.com/olbrich/ruby-units](http://github.com/olbrich/ruby-units)
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
Introduction
|
11
|
+
------------
|
12
|
+
|
13
|
+
Many technical applications make use of specialized calculations at some point. Frequently, these calculations require unit conversions to ensure accurate results. Needless to say, this is a pain to properly keep track of, and is prone to numerous errors.
|
14
|
+
|
15
|
+
Solution
|
16
|
+
--------
|
17
|
+
|
13
18
|
The 'Ruby units' gem is designed to simplify the handling of units for scientific calculations. The units of each quantity are specified when a Unit object is created and the Unit class will handle all subsequent conversions and manipulations to ensure an accurate result.
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
19
|
+
|
20
|
+
Installation:
|
21
|
+
-------------
|
22
|
+
|
23
|
+
This package may be installed using: `gem install ruby-units`
|
24
|
+
|
25
|
+
Usage:
|
26
|
+
------
|
27
|
+
|
28
|
+
```
|
29
|
+
unit = Unit.new("1") # constant only
|
30
|
+
unit = Unit.new("mm") # unit only (defaults to a scalar of 1)
|
31
|
+
unit = Unit.new("1 mm") # create a simple unit
|
32
|
+
unit = Unit.new("1 mm/s") # a compound unit
|
33
|
+
unit = Unit.new("1 mm s^-1") # in exponent notation
|
34
|
+
unit = Unit.new("1 kg*m^2/s^2") # complex unit
|
35
|
+
unit = Unit.new("1 kg m^2 s^-2") # complex unit
|
36
|
+
unit = Unit.new("1 mm") # shorthand
|
37
|
+
unit = "1 mm".to_unit # convert string object
|
38
|
+
unit = object.to_unit # convert any object using object.to_s
|
39
|
+
unit = Unit.new('1/4 cup') # Rational number
|
40
|
+
unit = Unit.new('1+1i mm') # Complex Number
|
41
|
+
```
|
42
|
+
|
43
|
+
Rules:
|
44
|
+
------
|
45
|
+
|
46
|
+
1. only 1 quantity per unit (with 2 exceptions... 6'5" and '8 lbs 8 oz')
|
47
|
+
2. use SI notation when possible
|
48
|
+
3. spaces in units are allowed, but ones like '11/m' will be recognized as '11 1/m'.
|
49
|
+
|
50
|
+
Unit compatibility:
|
51
|
+
-------------------
|
52
|
+
|
53
|
+
Many methods require that the units of two operands are compatible. Compatible units are those that can be easily converted into each other, such as 'meters' and 'feet'.
|
54
|
+
|
55
|
+
```
|
56
|
+
unit1 =~ unit2 #=> true if units are compatible
|
57
|
+
unit1.compatible?(unit2) #=> true if units are compatible
|
58
|
+
```
|
59
|
+
|
60
|
+
Unit Math:
|
61
|
+
----------
|
62
|
+
|
63
|
+
```
|
64
|
+
Unit#+() # Add. only works if units are compatible
|
65
|
+
Unit#-() # Subtract. only works if units are compatible
|
66
|
+
Unit#*() # Multiply.
|
67
|
+
Unit#/() # Divide.
|
68
|
+
Unit#**() # Exponentiate. Exponent must be an integer, can be positive, negative, or zero
|
69
|
+
Unit#inverse # Returns 1/unit
|
70
|
+
Unit#abs # Returns absolute value of the unit quantity. Strips off the units
|
71
|
+
Unit#ceil # rounds quantity to next highest integer
|
72
|
+
Unit#floor # rounds quantity down to next lower integer
|
73
|
+
Unit#round # rounds quantity to nearest integer
|
74
|
+
Unit#to_int # returns the quantity as an integer
|
75
|
+
```
|
76
|
+
|
77
|
+
Unit will coerce other objects into a Unit if used in a formula. This means that ..
|
78
|
+
|
79
|
+
```
|
80
|
+
Unit.new("1 mm") + "2 mm" == Unit.new("3 mm")
|
81
|
+
```
|
82
|
+
|
83
|
+
This will work as expected so long as you start the formula with a Unit object.
|
84
|
+
|
85
|
+
Conversions & comparisons
|
86
|
+
-------------------------
|
87
|
+
|
63
88
|
Units can be converted to other units in a couple of ways.
|
64
89
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
90
|
+
```
|
91
|
+
unit.convert_to('ft') # convert
|
92
|
+
unit1 = unit >> "ft" # convert to 'feet'
|
93
|
+
unit >>= "ft" # convert and overwrite original object
|
94
|
+
unit3 = unit1 + unit2 # resulting object will have the units of unit1
|
95
|
+
unit3 = unit1 - unit2 # resulting object will have the units of unit1
|
96
|
+
unit1 <=> unit2 # does comparison on quantities in base units, throws an exception if not compatible
|
97
|
+
unit1 === unit2 # true if units and quantity are the same, even if 'equivalent' by <=>
|
98
|
+
unit1 + unit2 >> "ft" # converts result of math to 'ft'
|
99
|
+
(unit1 + unit2).convert_to('ft') # converts result to 'ft'
|
100
|
+
```
|
101
|
+
|
75
102
|
Any object that defines a 'to_unit' method will be automatically coerced to a unit during calculations.
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
103
|
+
|
104
|
+
Text Output
|
105
|
+
-----------
|
106
|
+
|
107
|
+
Units will display themselves nicely based on the display_name for the units and prefixes. Since Unit implements a Unit#to_s, all that is needed in most cases is:
|
108
|
+
|
109
|
+
```
|
110
|
+
"#{Unit.new('1 mm')}" #=> "1 mm"
|
111
|
+
```
|
112
|
+
|
83
113
|
The to_s also accepts some options.
|
84
114
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
115
|
+
```
|
116
|
+
Unit.new('1.5 mm').to_s("%0.2f") # "1.50 mm". Enter any valid format
|
117
|
+
string. Also accepts strftime format
|
118
|
+
Unit.new('1.5 mm').to_s("in") # converts to inches before printing
|
119
|
+
Unit.new("2 m").to_s(:ft) # returns 6'7"
|
120
|
+
Unit.new("100 kg").to_s(:lbs) # returns 220 lbs, 7 oz
|
121
|
+
```
|
122
|
+
|
123
|
+
Time Helpers
|
124
|
+
------------
|
90
125
|
|
91
|
-
|
92
|
-
## Time Helpers
|
93
126
|
Time, Date, and DateTime objects can have time units added or subtracted.
|
94
127
|
|
95
|
-
|
128
|
+
```
|
129
|
+
Time.now + Unit.new("10 min")
|
130
|
+
```
|
96
131
|
|
97
|
-
Several helpers have also been defined.
|
98
|
-
Note: If you include the 'Chronic' gem, you can specify times in natural
|
99
|
-
language.
|
132
|
+
Several helpers have also been defined. Note: If you include the 'Chronic' gem, you can specify times in natural language.
|
100
133
|
|
101
|
-
|
134
|
+
```
|
135
|
+
Unit.new('min').since(DateTime.parse('9/18/06 3:00pm'))
|
136
|
+
```
|
102
137
|
|
103
138
|
Durations may be entered as 'HH:MM:SS, usec' and will be returned in 'hours'.
|
104
139
|
|
105
|
-
|
106
|
-
|
107
|
-
|
140
|
+
```
|
141
|
+
Unit.new('1:00') #=> 1 h
|
142
|
+
Unit.new('0:30') #=> 0.5 h
|
143
|
+
Unit.new('0:30:30') #=> 0.5 h + 30 sec
|
144
|
+
```
|
108
145
|
|
109
146
|
If only one ":" is present, it is interpreted as the separator between hours and minutes.
|
110
147
|
|
111
|
-
|
112
|
-
|
148
|
+
Ranges
|
149
|
+
------
|
150
|
+
|
151
|
+
```
|
152
|
+
[Unit.new('0 h')..Unit.new('10 h')].each {|x| p x}
|
153
|
+
```
|
154
|
+
|
113
155
|
works so long as the starting point has an integer scalar
|
114
156
|
|
115
|
-
|
116
|
-
|
157
|
+
Math functions
|
158
|
+
--------------
|
159
|
+
|
160
|
+
All Trig math functions (sin, cos, sinh, hypot...) can take a unit as their parameter. It will be converted to radians and then used if possible.
|
161
|
+
|
162
|
+
Temperatures
|
163
|
+
------------
|
117
164
|
|
118
|
-
## Temperatures
|
119
165
|
Ruby-units makes a distinction between a temperature (which technically is a property) and degrees of temperature (which temperatures are measured in).
|
120
166
|
|
121
|
-
Temperature units (i.e., 'tempK') can be converted back and forth, and will take into account the differences in the zero points of the various scales. Differential temperature (e.g., '100 degC'
|
167
|
+
Temperature units (i.e., 'tempK') can be converted back and forth, and will take into account the differences in the zero points of the various scales. Differential temperature (e.g., Unit.new('100 degC')) units behave like most other units.
|
122
168
|
|
123
|
-
|
169
|
+
```
|
170
|
+
Unit.new('37 tempC').convert_to('tempF') #=> 98.6 tempF
|
171
|
+
```
|
124
172
|
|
125
173
|
Ruby-units will raise an exception if you attempt to create a temperature unit that would fall below absolute zero.
|
126
174
|
|
127
|
-
Unit math on temperatures is fairly limited.
|
175
|
+
Unit math on temperatures is fairly limited.
|
128
176
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
177
|
+
```
|
178
|
+
Unit.new('100 tempC') + Unit.new('10 degC') # '110 tempC'.to_unit
|
179
|
+
Unit.new('100 tempC') - Unit.new('10 degC') # '90 tempC'.to_unit
|
180
|
+
Unit.new('100 tempC') + Unit.new('50 tempC') # exception (can't add two temperatures)
|
181
|
+
Unit.new('100 tempC') - Unit.new('50 tempC') # '50 degC'.to_unit (get the difference between two temperatures)
|
182
|
+
Unit.new('50 tempC') - Unit.new('100 tempC') # '-50 degC'.to_unit
|
183
|
+
Unit.new('100 tempC') * scalar # '100*scalar tempC'.to_unit
|
184
|
+
Unit.new('100 tempC') / scalar # '100/scalar tempC'.to_unit
|
185
|
+
Unit.new('100 tempC') * unit # exception
|
186
|
+
Unit.new('100 tempC') / unit # exception
|
187
|
+
Unit.new('100 tempC') ** N # exception
|
139
188
|
|
140
|
-
|
141
|
-
|
189
|
+
Unit.new('100 tempC').convert_to('degC') #=> Unit.new('100 degC')
|
190
|
+
```
|
142
191
|
|
143
|
-
|
144
|
-
These conversions are always interpreted as being relative to absolute zero.
|
145
|
-
Conversions are probably better done like this...
|
146
|
-
|
147
|
-
Unit('0 tempC') + Unit('100 degC') #=> Unit('100 tempC')
|
192
|
+
This conversion references the 0 point on the scale of the temperature unit
|
148
193
|
|
149
|
-
|
194
|
+
```
|
195
|
+
Unit.new('100 degC').convert_to('tempC') #=> '-173 tempC'.to_unit
|
196
|
+
```
|
197
|
+
|
198
|
+
These conversions are always interpreted as being relative to absolute zero. Conversions are probably better done like this...
|
199
|
+
|
200
|
+
```
|
201
|
+
Unit.new('0 tempC') + Unit.new('100 degC') #=> Unit.new('100 tempC')
|
202
|
+
```
|
203
|
+
|
204
|
+
Defining Units
|
205
|
+
--------------
|
150
206
|
|
151
207
|
It is possible to define new units or redefine existing ones.
|
152
208
|
|
@@ -154,33 +210,38 @@ It is possible to define new units or redefine existing ones.
|
|
154
210
|
|
155
211
|
The easiest approach is to define a unit in terms of other units.
|
156
212
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
213
|
+
```
|
214
|
+
Unit.define("foobar") do |foobar|
|
215
|
+
foobar.definition = Unit.new("1 foo") * Unit.new("1 bar") # anything that results in a Unit object
|
216
|
+
foobar.aliases = %w{foobar fb} # array of synonyms for the unit
|
217
|
+
foobar.display_name = "Foobar" # How unit is displayed when output
|
218
|
+
end
|
219
|
+
```
|
164
220
|
|
165
|
-
|
166
|
-
This is useful for changing display names, adding aliases, etc.
|
221
|
+
### Redefine Existing Unit
|
167
222
|
|
168
|
-
|
169
|
-
cup.display_name = "cup"
|
170
|
-
end
|
223
|
+
Redefining a unit allows the user to change a single aspect of a definition without having to re-create the entire definition. This is useful for changing display names, adding aliases, etc.
|
171
224
|
|
225
|
+
```
|
226
|
+
Unit.redefine!("cup") do |cup|
|
227
|
+
cup.display_name = "cup"
|
228
|
+
end
|
229
|
+
```
|
172
230
|
|
173
231
|
### Namespaced Class
|
174
232
|
|
175
|
-
Sometimes the default class 'Unit' may conflict with other gems or applications.
|
176
|
-
The actual class of a unit is the RubyUnits::Unit. For simplicity and backwards compatiblity, the '::Unit' class is defined as an alias to '::RubyUnits::Unit'.
|
233
|
+
Sometimes the default class 'Unit' may conflict with other gems or applications. Internally ruby-units defines itself using the RubyUnits namespace. The actual class of a unit is the RubyUnits::Unit. For simplicity and backwards compatiblity, the '::Unit' class is defined as an alias to '::RubyUnits::Unit'.
|
177
234
|
|
178
235
|
To load ruby-units without this alias...
|
179
236
|
|
180
|
-
|
237
|
+
```
|
238
|
+
require 'ruby_units/namespaced'
|
239
|
+
```
|
181
240
|
|
182
241
|
When using bundler...
|
183
242
|
|
184
|
-
|
243
|
+
```
|
244
|
+
gem 'ruby-units', require: 'ruby_units/namespaced'
|
245
|
+
```
|
185
246
|
|
186
|
-
Note: when using the namespaced version, the Unit('unit string') helper will not be defined.
|
247
|
+
Note: when using the namespaced version, the Unit.new('unit string') helper will not be defined.
|