ruby-units 2.1.0 → 2.3.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 +5 -5
- data/.codeclimate.yml +32 -0
- data/.csslintrc +2 -0
- data/.eslintignore +1 -0
- data/.eslintrc +213 -0
- data/.github/workflows/tests.yml +40 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.rubocop.yml +24 -0
- data/.ruby-version +1 -0
- data/.solargraph.yml +16 -0
- data/CHANGELOG.txt +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +164 -0
- data/Guardfile +31 -0
- data/LICENSE.txt +18 -17
- data/README.md +124 -99
- data/Rakefile +3 -24
- data/lib/ruby_units/date.rb +2 -2
- data/lib/ruby_units/math.rb +1 -3
- data/lib/ruby_units/time.rb +2 -2
- data/lib/ruby_units/unit.rb +83 -54
- data/lib/ruby_units/unit_definitions/base.rb +28 -28
- data/lib/ruby_units/unit_definitions/prefix.rb +30 -32
- data/lib/ruby_units/unit_definitions/standard.rb +108 -108
- data/lib/ruby_units/version.rb +1 -4
- data/ruby-units.gemspec +35 -72
- metadata +121 -24
- data/VERSION +0 -1
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
ruby-units (2.3.2)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.1)
|
|
10
|
+
backport (1.1.2)
|
|
11
|
+
benchmark (0.1.1)
|
|
12
|
+
byebug (11.1.3)
|
|
13
|
+
coderay (1.1.3)
|
|
14
|
+
diff-lcs (1.4.4)
|
|
15
|
+
docile (1.3.4)
|
|
16
|
+
e2mmap (0.1.0)
|
|
17
|
+
ffi (1.14.2)
|
|
18
|
+
ffi (1.14.2-java)
|
|
19
|
+
formatador (0.2.5)
|
|
20
|
+
guard (2.16.2)
|
|
21
|
+
formatador (>= 0.2.4)
|
|
22
|
+
listen (>= 2.7, < 4.0)
|
|
23
|
+
lumberjack (>= 1.0.12, < 2.0)
|
|
24
|
+
nenv (~> 0.1)
|
|
25
|
+
notiffany (~> 0.0)
|
|
26
|
+
pry (>= 0.9.12)
|
|
27
|
+
shellany (~> 0.0)
|
|
28
|
+
thor (>= 0.18.1)
|
|
29
|
+
guard-compat (1.2.1)
|
|
30
|
+
guard-rspec (4.7.3)
|
|
31
|
+
guard (~> 2.1)
|
|
32
|
+
guard-compat (~> 1.1)
|
|
33
|
+
rspec (>= 2.99.0, < 4.0)
|
|
34
|
+
jaro_winkler (1.5.4)
|
|
35
|
+
jaro_winkler (1.5.4-java)
|
|
36
|
+
kramdown (2.3.0)
|
|
37
|
+
rexml
|
|
38
|
+
kramdown-parser-gfm (1.1.0)
|
|
39
|
+
kramdown (~> 2.0)
|
|
40
|
+
listen (3.3.3)
|
|
41
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
|
42
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
|
43
|
+
lumberjack (1.2.8)
|
|
44
|
+
method_source (1.0.0)
|
|
45
|
+
mini_portile2 (2.4.0)
|
|
46
|
+
nenv (0.3.0)
|
|
47
|
+
nokogiri (1.10.10)
|
|
48
|
+
mini_portile2 (~> 2.4.0)
|
|
49
|
+
nokogiri (1.10.10-java)
|
|
50
|
+
notiffany (0.1.3)
|
|
51
|
+
nenv (~> 0.1)
|
|
52
|
+
shellany (~> 0.0)
|
|
53
|
+
parallel (1.20.1)
|
|
54
|
+
parser (2.7.2.0)
|
|
55
|
+
ast (~> 2.4.1)
|
|
56
|
+
pry (0.13.1)
|
|
57
|
+
coderay (~> 1.1)
|
|
58
|
+
method_source (~> 1.0)
|
|
59
|
+
pry (0.13.1-java)
|
|
60
|
+
coderay (~> 1.1)
|
|
61
|
+
method_source (~> 1.0)
|
|
62
|
+
spoon (~> 0.0)
|
|
63
|
+
pry-byebug (3.9.0)
|
|
64
|
+
byebug (~> 11.0)
|
|
65
|
+
pry (~> 0.13.0)
|
|
66
|
+
rainbow (3.0.0)
|
|
67
|
+
rake (13.0.3)
|
|
68
|
+
rb-fsevent (0.10.4)
|
|
69
|
+
rb-inotify (0.10.1)
|
|
70
|
+
ffi (~> 1.0)
|
|
71
|
+
regexp_parser (2.0.3)
|
|
72
|
+
reverse_markdown (2.0.0)
|
|
73
|
+
nokogiri
|
|
74
|
+
rexml (3.2.4)
|
|
75
|
+
rspec (3.10.0)
|
|
76
|
+
rspec-core (~> 3.10.0)
|
|
77
|
+
rspec-expectations (~> 3.10.0)
|
|
78
|
+
rspec-mocks (~> 3.10.0)
|
|
79
|
+
rspec-core (3.10.1)
|
|
80
|
+
rspec-support (~> 3.10.0)
|
|
81
|
+
rspec-expectations (3.10.1)
|
|
82
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
83
|
+
rspec-support (~> 3.10.0)
|
|
84
|
+
rspec-mocks (3.10.1)
|
|
85
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
86
|
+
rspec-support (~> 3.10.0)
|
|
87
|
+
rspec-support (3.10.1)
|
|
88
|
+
rubocop (1.7.0)
|
|
89
|
+
parallel (~> 1.10)
|
|
90
|
+
parser (>= 2.7.1.5)
|
|
91
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
92
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
93
|
+
rexml
|
|
94
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
|
95
|
+
ruby-progressbar (~> 1.7)
|
|
96
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
|
97
|
+
rubocop-ast (1.3.0)
|
|
98
|
+
parser (>= 2.7.1.5)
|
|
99
|
+
rubocop-rake (0.5.1)
|
|
100
|
+
rubocop
|
|
101
|
+
rubocop-rspec (2.1.0)
|
|
102
|
+
rubocop (~> 1.0)
|
|
103
|
+
rubocop-ast (>= 1.1.0)
|
|
104
|
+
ruby-maven (3.3.12)
|
|
105
|
+
ruby-maven-libs (~> 3.3.9)
|
|
106
|
+
ruby-maven-libs (3.3.9)
|
|
107
|
+
ruby-prof (1.4.2)
|
|
108
|
+
ruby-progressbar (1.10.1)
|
|
109
|
+
shellany (0.0.1)
|
|
110
|
+
simplecov (0.20.0)
|
|
111
|
+
docile (~> 1.1)
|
|
112
|
+
simplecov-html (~> 0.11)
|
|
113
|
+
simplecov_json_formatter (~> 0.1)
|
|
114
|
+
simplecov-html (0.12.3)
|
|
115
|
+
simplecov_json_formatter (0.1.2)
|
|
116
|
+
solargraph (0.40.1)
|
|
117
|
+
backport (~> 1.1)
|
|
118
|
+
benchmark
|
|
119
|
+
bundler (>= 1.17.2)
|
|
120
|
+
e2mmap
|
|
121
|
+
jaro_winkler (~> 1.5)
|
|
122
|
+
kramdown (~> 2.3)
|
|
123
|
+
kramdown-parser-gfm (~> 1.1)
|
|
124
|
+
parser (~> 2.3)
|
|
125
|
+
reverse_markdown (>= 1.0.5, < 3)
|
|
126
|
+
rubocop (>= 0.52)
|
|
127
|
+
thor (~> 1.0)
|
|
128
|
+
tilt (~> 2.0)
|
|
129
|
+
yard (~> 0.9, >= 0.9.24)
|
|
130
|
+
spoon (0.0.6)
|
|
131
|
+
ffi
|
|
132
|
+
terminal-notifier (2.0.0)
|
|
133
|
+
terminal-notifier-guard (1.7.0)
|
|
134
|
+
thor (1.0.1)
|
|
135
|
+
tilt (2.0.10)
|
|
136
|
+
unicode-display_width (1.7.0)
|
|
137
|
+
yard (0.9.26)
|
|
138
|
+
|
|
139
|
+
PLATFORMS
|
|
140
|
+
java
|
|
141
|
+
ruby
|
|
142
|
+
universal-java-11
|
|
143
|
+
x86_64-darwin-19
|
|
144
|
+
|
|
145
|
+
DEPENDENCIES
|
|
146
|
+
guard-rspec
|
|
147
|
+
pry
|
|
148
|
+
pry-byebug
|
|
149
|
+
rake
|
|
150
|
+
rspec (~> 3.0)
|
|
151
|
+
rubocop
|
|
152
|
+
rubocop-rake
|
|
153
|
+
rubocop-rspec
|
|
154
|
+
ruby-maven
|
|
155
|
+
ruby-prof
|
|
156
|
+
ruby-units!
|
|
157
|
+
simplecov
|
|
158
|
+
simplecov-html
|
|
159
|
+
solargraph
|
|
160
|
+
terminal-notifier
|
|
161
|
+
terminal-notifier-guard
|
|
162
|
+
|
|
163
|
+
BUNDLED WITH
|
|
164
|
+
2.2.3
|
data/Guardfile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# A sample Guardfile
|
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
|
3
|
+
|
|
4
|
+
## Uncomment and set this to only include directories you want to watch
|
|
5
|
+
directories %w[lib spec]
|
|
6
|
+
|
|
7
|
+
## Uncomment to clear the screen before every task
|
|
8
|
+
clearing :on
|
|
9
|
+
|
|
10
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
|
11
|
+
# rspec may be run, below are examples of the most common uses.
|
|
12
|
+
# * bundler: 'bundle exec rspec'
|
|
13
|
+
# * bundler binstubs: 'bin/rspec'
|
|
14
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
|
15
|
+
# installed the spring binstubs per the docs)
|
|
16
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
|
17
|
+
# * 'just' rspec: 'rspec'
|
|
18
|
+
|
|
19
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
|
20
|
+
require 'ostruct'
|
|
21
|
+
|
|
22
|
+
# Generic Ruby apps
|
|
23
|
+
rspec = OpenStruct.new
|
|
24
|
+
rspec.spec = ->(m) { "spec/#{m}_spec.rb" }
|
|
25
|
+
rspec.spec_dir = 'spec'
|
|
26
|
+
rspec.spec_helper = 'spec/spec_helper.rb'
|
|
27
|
+
|
|
28
|
+
watch(%r{^spec/.+_spec\.rb$})
|
|
29
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| rspec.spec.call(m[1]) }
|
|
30
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
|
31
|
+
end
|
data/LICENSE.txt
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
a copy of this software and associated documentation files (the
|
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
-
the following conditions:
|
|
3
|
+
Copyright (c) 2019 Kevin Olbrich
|
|
10
4
|
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
13
11
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
|
@@ -1,31 +1,45 @@
|
|
|
1
|
-
Ruby Units
|
|
2
|
-
==========
|
|
1
|
+
# Ruby Units
|
|
3
2
|
|
|
4
|
-
[](https://codeclimate.com/github/olbrich/ruby-units/maintainability)
|
|
4
|
+
[](https://codeclimate.com/github/olbrich/ruby-units/test_coverage)
|
|
5
|
+
[](https://app.fossa.io/projects/git%2Bgithub.com%2Folbrich%2Fruby-units?ref=badge_shield)
|
|
5
6
|
|
|
6
7
|
Kevin C. Olbrich, Ph.D.
|
|
7
8
|
|
|
8
|
-
Project page:
|
|
9
|
+
Project page:
|
|
10
|
+
[http://github.com/olbrich/ruby-units](http://github.com/olbrich/ruby-units)
|
|
9
11
|
|
|
10
|
-
Introduction
|
|
11
|
-
------------
|
|
12
|
+
## Introduction
|
|
12
13
|
|
|
13
|
-
Many technical applications make use of specialized calculations at some point.
|
|
14
|
+
Many technical applications make use of specialized calculations at some point.
|
|
15
|
+
Frequently, these calculations require unit conversions to ensure accurate
|
|
16
|
+
results. Needless to say, this is a pain to properly keep track of, and is prone
|
|
17
|
+
to numerous errors.
|
|
14
18
|
|
|
15
|
-
Solution
|
|
16
|
-
--------
|
|
19
|
+
## Solution
|
|
17
20
|
|
|
18
|
-
The 'Ruby units' gem is designed to simplify the handling of units for
|
|
21
|
+
The 'Ruby units' gem is designed to simplify the handling of units for
|
|
22
|
+
scientific calculations. The units of each quantity are specified when a Unit
|
|
23
|
+
object is created and the Unit class will handle all subsequent conversions and
|
|
24
|
+
manipulations to ensure an accurate result.
|
|
19
25
|
|
|
20
|
-
Installation
|
|
21
|
-
-------------
|
|
26
|
+
## Installation
|
|
22
27
|
|
|
23
|
-
This package may be installed using:
|
|
28
|
+
This package may be installed using:
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
|
|
30
|
+
```bash
|
|
31
|
+
gem install ruby-units
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
or add this to your `Gemfile`
|
|
27
35
|
|
|
36
|
+
```ruby
|
|
37
|
+
gem 'ruby-units'
|
|
28
38
|
```
|
|
39
|
+
|
|
40
|
+
## Usage
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
29
43
|
unit = Unit.new("1") # constant only
|
|
30
44
|
unit = Unit.new("mm") # unit only (defaults to a scalar of 1)
|
|
31
45
|
unit = Unit.new("1 mm") # create a simple unit
|
|
@@ -40,32 +54,32 @@ unit = Unit.new('1/4 cup') # Rational number
|
|
|
40
54
|
unit = Unit.new('1+1i mm') # Complex Number
|
|
41
55
|
```
|
|
42
56
|
|
|
43
|
-
Rules
|
|
44
|
-
------
|
|
57
|
+
### Rules
|
|
45
58
|
|
|
46
|
-
1.
|
|
47
|
-
2.
|
|
48
|
-
3.
|
|
59
|
+
1. only 1 quantity per unit (with 2 exceptions... 6'5" and '8 lbs 8 oz')
|
|
60
|
+
2. use SI notation when possible
|
|
61
|
+
3. spaces in units are allowed, but ones like '11/m' will be recognized as '11
|
|
62
|
+
1/m'.
|
|
49
63
|
|
|
50
|
-
Unit compatibility
|
|
51
|
-
-------------------
|
|
64
|
+
### Unit compatibility
|
|
52
65
|
|
|
53
|
-
Many methods require that the units of two operands are compatible. Compatible
|
|
66
|
+
Many methods require that the units of two operands are compatible. Compatible
|
|
67
|
+
units are those that can be easily converted into each other, such as 'meters'
|
|
68
|
+
and 'feet'.
|
|
54
69
|
|
|
55
|
-
```
|
|
70
|
+
```ruby
|
|
56
71
|
unit1 =~ unit2 #=> true if units are compatible
|
|
57
72
|
unit1.compatible?(unit2) #=> true if units are compatible
|
|
58
73
|
```
|
|
59
74
|
|
|
60
|
-
Unit Math
|
|
61
|
-
----------
|
|
75
|
+
### Unit Math
|
|
62
76
|
|
|
63
|
-
```
|
|
77
|
+
```text
|
|
64
78
|
Unit#+() # Add. only works if units are compatible
|
|
65
79
|
Unit#-() # Subtract. only works if units are compatible
|
|
66
|
-
Unit#*() # Multiply.
|
|
80
|
+
Unit#*() # Multiply.
|
|
67
81
|
Unit#/() # Divide.
|
|
68
|
-
Unit#**() # Exponentiate. Exponent must be an integer, can be positive, negative, or zero
|
|
82
|
+
Unit#**() # Exponentiate. Exponent must be an integer, can be positive, negative, or zero
|
|
69
83
|
Unit#inverse # Returns 1/unit
|
|
70
84
|
Unit#abs # Returns absolute value of the unit quantity. Strips off the units
|
|
71
85
|
Unit#ceil # rounds quantity to next highest integer
|
|
@@ -74,20 +88,20 @@ Unit#round # rounds quantity to nearest integer
|
|
|
74
88
|
Unit#to_int # returns the quantity as an integer
|
|
75
89
|
```
|
|
76
90
|
|
|
77
|
-
Unit will coerce other objects into a Unit if used in a formula. This means
|
|
91
|
+
Unit will coerce other objects into a Unit if used in a formula. This means...
|
|
78
92
|
|
|
79
|
-
```
|
|
93
|
+
```ruby
|
|
80
94
|
Unit.new("1 mm") + "2 mm" == Unit.new("3 mm")
|
|
81
95
|
```
|
|
82
96
|
|
|
83
|
-
This will work as expected so long as you start the formula with a Unit
|
|
97
|
+
This will work as expected so long as you start the formula with a `Unit`
|
|
98
|
+
object.
|
|
84
99
|
|
|
85
|
-
Conversions &
|
|
86
|
-
-------------------------
|
|
100
|
+
### Conversions & Comparisons
|
|
87
101
|
|
|
88
102
|
Units can be converted to other units in a couple of ways.
|
|
89
103
|
|
|
90
|
-
```
|
|
104
|
+
```ruby
|
|
91
105
|
unit.convert_to('ft') # convert
|
|
92
106
|
unit1 = unit >> "ft" # convert to 'feet'
|
|
93
107
|
unit >>= "ft" # convert and overwrite original object
|
|
@@ -99,83 +113,89 @@ unit1 + unit2 >> "ft" # converts result of math to 'ft'
|
|
|
99
113
|
(unit1 + unit2).convert_to('ft') # converts result to 'ft'
|
|
100
114
|
```
|
|
101
115
|
|
|
102
|
-
Any object that defines a
|
|
116
|
+
Any object that defines a `to_unit` method will be automatically coerced to a
|
|
117
|
+
unit during calculations.
|
|
103
118
|
|
|
104
|
-
Text Output
|
|
105
|
-
-----------
|
|
119
|
+
### Text Output
|
|
106
120
|
|
|
107
|
-
Units will display themselves nicely based on the display_name for the units and
|
|
121
|
+
Units will display themselves nicely based on the display_name for the units and
|
|
122
|
+
prefixes. Since `Unit` implements a `Unit#to_s`, all that is needed in most
|
|
123
|
+
cases is:
|
|
108
124
|
|
|
109
|
-
```
|
|
125
|
+
```ruby
|
|
110
126
|
"#{Unit.new('1 mm')}" #=> "1 mm"
|
|
111
127
|
```
|
|
112
128
|
|
|
113
|
-
The to_s also accepts some options.
|
|
129
|
+
The `to_s` also accepts some options.
|
|
114
130
|
|
|
115
|
-
```
|
|
131
|
+
```ruby
|
|
116
132
|
Unit.new('1.5 mm').to_s("%0.2f") # "1.50 mm". Enter any valid format
|
|
117
|
-
|
|
133
|
+
# string. Also accepts strftime format
|
|
118
134
|
Unit.new('1.5 mm').to_s("in") # converts to inches before printing
|
|
119
135
|
Unit.new("2 m").to_s(:ft) # returns 6'7"
|
|
120
136
|
Unit.new("100 kg").to_s(:lbs) # returns 220 lbs, 7 oz
|
|
121
137
|
Unit.new("100 kg").to_s(:stone) # returns 15 stone, 10 lb
|
|
122
138
|
```
|
|
123
139
|
|
|
124
|
-
Time Helpers
|
|
125
|
-
------------
|
|
140
|
+
### Time Helpers
|
|
126
141
|
|
|
127
|
-
Time
|
|
142
|
+
`Time`, `Date`, and `DateTime` objects can have time units added or subtracted.
|
|
128
143
|
|
|
129
|
-
```
|
|
144
|
+
```ruby
|
|
130
145
|
Time.now + Unit.new("10 min")
|
|
131
146
|
```
|
|
132
147
|
|
|
133
|
-
Several helpers have also been defined. Note: If you include the 'Chronic' gem,
|
|
148
|
+
Several helpers have also been defined. Note: If you include the 'Chronic' gem,
|
|
149
|
+
you can specify times in natural language.
|
|
134
150
|
|
|
135
|
-
```
|
|
136
|
-
|
|
151
|
+
```ruby
|
|
152
|
+
Unit.new('min').since(DateTime.parse('9/18/06 3:00pm'))
|
|
137
153
|
```
|
|
138
154
|
|
|
139
155
|
Durations may be entered as 'HH:MM:SS, usec' and will be returned in 'hours'.
|
|
140
156
|
|
|
141
|
-
```
|
|
157
|
+
```ruby
|
|
142
158
|
Unit.new('1:00') #=> 1 h
|
|
143
159
|
Unit.new('0:30') #=> 0.5 h
|
|
144
160
|
Unit.new('0:30:30') #=> 0.5 h + 30 sec
|
|
145
161
|
```
|
|
146
162
|
|
|
147
|
-
If only one ":" is present, it is interpreted as the separator between hours and
|
|
163
|
+
If only one ":" is present, it is interpreted as the separator between hours and
|
|
164
|
+
minutes.
|
|
148
165
|
|
|
149
|
-
Ranges
|
|
150
|
-
------
|
|
166
|
+
### Ranges
|
|
151
167
|
|
|
152
|
-
```
|
|
168
|
+
```ruby
|
|
153
169
|
[Unit.new('0 h')..Unit.new('10 h')].each {|x| p x}
|
|
154
170
|
```
|
|
155
171
|
|
|
156
172
|
works so long as the starting point has an integer scalar
|
|
157
173
|
|
|
158
|
-
Math functions
|
|
159
|
-
--------------
|
|
174
|
+
### Math functions
|
|
160
175
|
|
|
161
|
-
All Trig math functions (sin, cos, sinh, hypot...) can take a unit as their
|
|
176
|
+
All Trig math functions (sin, cos, sinh, hypot...) can take a unit as their
|
|
177
|
+
parameter. It will be converted to radians and then used if possible.
|
|
162
178
|
|
|
163
|
-
Temperatures
|
|
164
|
-
------------
|
|
179
|
+
### Temperatures
|
|
165
180
|
|
|
166
|
-
Ruby-units makes a distinction between a temperature (which technically is a
|
|
181
|
+
Ruby-units makes a distinction between a temperature (which technically is a
|
|
182
|
+
property) and degrees of temperature (which temperatures are measured in).
|
|
167
183
|
|
|
168
|
-
Temperature units (i.e., 'tempK') can be converted back and forth, and will take
|
|
184
|
+
Temperature units (i.e., 'tempK') can be converted back and forth, and will take
|
|
185
|
+
into account the differences in the zero points of the various scales.
|
|
186
|
+
Differential temperature (e.g., Unit.new('100 degC')) units behave like most
|
|
187
|
+
other units.
|
|
169
188
|
|
|
170
|
-
```
|
|
189
|
+
```ruby
|
|
171
190
|
Unit.new('37 tempC').convert_to('tempF') #=> 98.6 tempF
|
|
172
191
|
```
|
|
173
192
|
|
|
174
|
-
Ruby-units will raise an exception if you attempt to create a temperature unit
|
|
193
|
+
Ruby-units will raise an exception if you attempt to create a temperature unit
|
|
194
|
+
that would fall below absolute zero.
|
|
175
195
|
|
|
176
196
|
Unit math on temperatures is fairly limited.
|
|
177
197
|
|
|
178
|
-
```
|
|
198
|
+
```ruby
|
|
179
199
|
Unit.new('100 tempC') + Unit.new('10 degC') # '110 tempC'.to_unit
|
|
180
200
|
Unit.new('100 tempC') - Unit.new('10 degC') # '90 tempC'.to_unit
|
|
181
201
|
Unit.new('100 tempC') + Unit.new('50 tempC') # exception (can't add two temperatures)
|
|
@@ -187,31 +207,31 @@ Unit.new('100 tempC') * unit # exception
|
|
|
187
207
|
Unit.new('100 tempC') / unit # exception
|
|
188
208
|
Unit.new('100 tempC') ** N # exception
|
|
189
209
|
|
|
190
|
-
Unit.new('100 tempC').convert_to('degC') #=> Unit.new('100 degC')
|
|
210
|
+
Unit.new('100 tempC').convert_to('degC') #=> Unit.new('100 degC')
|
|
191
211
|
```
|
|
192
212
|
|
|
193
213
|
This conversion references the 0 point on the scale of the temperature unit
|
|
194
214
|
|
|
195
|
-
```
|
|
215
|
+
```ruby
|
|
196
216
|
Unit.new('100 degC').convert_to('tempC') #=> '-173 tempC'.to_unit
|
|
197
217
|
```
|
|
198
218
|
|
|
199
|
-
These conversions are always interpreted as being relative to absolute zero.
|
|
219
|
+
These conversions are always interpreted as being relative to absolute zero.
|
|
220
|
+
Conversions are probably better done like this...
|
|
200
221
|
|
|
201
|
-
```
|
|
222
|
+
```ruby
|
|
202
223
|
Unit.new('0 tempC') + Unit.new('100 degC') #=> Unit.new('100 tempC')
|
|
203
224
|
```
|
|
204
225
|
|
|
205
|
-
Defining Units
|
|
206
|
-
--------------
|
|
226
|
+
### Defining Units
|
|
207
227
|
|
|
208
228
|
It is possible to define new units or redefine existing ones.
|
|
209
229
|
|
|
210
|
-
|
|
230
|
+
#### Define New Unit
|
|
211
231
|
|
|
212
232
|
The easiest approach is to define a unit in terms of other units.
|
|
213
233
|
|
|
214
|
-
```
|
|
234
|
+
```ruby
|
|
215
235
|
Unit.define("foobar") do |foobar|
|
|
216
236
|
foobar.definition = Unit.new("1 foo") * Unit.new("1 bar") # anything that results in a Unit object
|
|
217
237
|
foobar.aliases = %w{foobar fb} # array of synonyms for the unit
|
|
@@ -219,11 +239,13 @@ Unit.define("foobar") do |foobar|
|
|
|
219
239
|
end
|
|
220
240
|
```
|
|
221
241
|
|
|
222
|
-
|
|
242
|
+
#### Redefine Existing Unit
|
|
223
243
|
|
|
224
|
-
Redefining a unit allows the user to change a single aspect of a definition
|
|
244
|
+
Redefining a unit allows the user to change a single aspect of a definition
|
|
245
|
+
without having to re-create the entire definition. This is useful for changing
|
|
246
|
+
display names, adding aliases, etc.
|
|
225
247
|
|
|
226
|
-
```
|
|
248
|
+
```ruby
|
|
227
249
|
Unit.redefine!("cup") do |cup|
|
|
228
250
|
cup.display_name = "cup"
|
|
229
251
|
end
|
|
@@ -231,39 +253,46 @@ end
|
|
|
231
253
|
|
|
232
254
|
### Useful methods
|
|
233
255
|
|
|
234
|
-
1. `scalar` will return the numeric portion of the unit without the attached
|
|
256
|
+
1. `scalar` will return the numeric portion of the unit without the attached
|
|
257
|
+
units
|
|
235
258
|
2. `base_scalar` will return the scalar in base units (SI)
|
|
236
259
|
3. `units` will return the name of the units (without the scalar)
|
|
237
260
|
4. `base` will return the unit converted to base units (SI)
|
|
238
261
|
|
|
239
262
|
### Storing in a database
|
|
240
263
|
|
|
241
|
-
Units can be stored in a database as either the string representation or in two
|
|
242
|
-
|
|
264
|
+
Units can be stored in a database as either the string representation or in two
|
|
265
|
+
separate columns defining the scalar and the units. Note that if sorting by
|
|
266
|
+
units is desired you will want to ensure that you are storing the scalars in a
|
|
267
|
+
consistent unit (i.e, the base units).
|
|
243
268
|
|
|
244
269
|
### Namespaced Class
|
|
245
270
|
|
|
246
|
-
Sometimes the default class 'Unit' may conflict with other gems or applications.
|
|
271
|
+
Sometimes the default class 'Unit' may conflict with other gems or applications.
|
|
272
|
+
Internally ruby-units defines itself using the RubyUnits namespace. The actual
|
|
273
|
+
class of a unit is the RubyUnits::Unit. For simplicity and backwards
|
|
274
|
+
compatibility, the `::Unit` class is defined as an alias to `::RubyUnits::Unit`.
|
|
247
275
|
|
|
248
276
|
To load ruby-units without this alias...
|
|
249
277
|
|
|
250
|
-
```
|
|
278
|
+
```ruby
|
|
251
279
|
require 'ruby_units/namespaced'
|
|
252
280
|
```
|
|
253
281
|
|
|
254
282
|
When using bundler...
|
|
255
283
|
|
|
256
|
-
```
|
|
284
|
+
```ruby
|
|
257
285
|
gem 'ruby-units', require: 'ruby_units/namespaced'
|
|
258
286
|
```
|
|
259
287
|
|
|
260
|
-
Note: when using the namespaced version, the Unit.new('unit string') helper
|
|
288
|
+
Note: when using the namespaced version, the `Unit.new('unit string')` helper
|
|
289
|
+
will not be defined.
|
|
261
290
|
|
|
262
291
|
### Configuration
|
|
263
292
|
|
|
264
293
|
Configuration options can be set like:
|
|
265
294
|
|
|
266
|
-
```
|
|
295
|
+
```ruby
|
|
267
296
|
RubyUnits.configure do |config|
|
|
268
297
|
config.separator = false
|
|
269
298
|
end
|
|
@@ -271,24 +300,20 @@ end
|
|
|
271
300
|
|
|
272
301
|
Currently there is only one configuration you can set:
|
|
273
302
|
|
|
274
|
-
1. separator (true/false): should a space be used to separate the scalar from
|
|
275
|
-
|
|
303
|
+
1. separator (true/false): should a space be used to separate the scalar from
|
|
304
|
+
the unit part during output.
|
|
276
305
|
|
|
277
306
|
### NOTES
|
|
278
307
|
|
|
279
|
-
####
|
|
280
|
-
|
|
281
|
-
Note that the current implementation of ruby-units requires 'mathn' from the ruby standard library.
|
|
282
|
-
This tends to change the behavior of integer math in ways that many people do not expect, and can be the source
|
|
283
|
-
of numerous bugs and odd behaviors. If you encounter what appears to be a bug in your code that seems to be related
|
|
284
|
-
|
|
285
|
-
to the use of ruby-units, try to reproduce the bug by just including 'mathn' by itself.
|
|
308
|
+
#### Performance vs. Accuracy
|
|
286
309
|
|
|
287
|
-
|
|
310
|
+
Ruby units was originally intended to provide a robust and accurate way to do
|
|
311
|
+
arbitrary unit conversions. In some cases, these conversions can result in the
|
|
312
|
+
creation and garbage collection of a lot of intermediate objects during
|
|
313
|
+
calculations. This in turn can have a negative impact on performance. The design
|
|
314
|
+
of ruby-units has emphasized accuracy over speed. YMMV if you are doing a lot of
|
|
315
|
+
math involving units.
|
|
288
316
|
|
|
289
|
-
|
|
317
|
+
## License
|
|
290
318
|
|
|
291
|
-
|
|
292
|
-
In some cases, these conversions can result in the creation and garbage collection of a lot of intermediate objects during
|
|
293
|
-
calculations. This in turn can have a negative impact on performance. The design of ruby-units has emphasized accuracy
|
|
294
|
-
over speed. YMMV if you are doing a lot of math involving units.
|
|
319
|
+
[](https://app.fossa.io/projects/git%2Bgithub.com%2Folbrich%2Fruby-units?ref=badge_large)
|