ruby-units 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +2 -0
- data/Manifest.txt +1 -1
- data/README.md +158 -0
- data/Rakefile +6 -1
- data/lib/ruby_units/ruby-units.rb +10 -9
- data/test/test_ruby-units.rb +12 -4
- metadata +30 -14
- data/README.txt +0 -174
data/CHANGELOG.txt
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
Change Log for Ruby-units
|
2
2
|
=========================
|
3
|
+
2010-03-16 1.1.5 * another bugfix, and update url to point to github
|
4
|
+
2010-03-15 1.1.4 * fixed a couple of outstanding bugs
|
3
5
|
2007-12-13 1.1.3 * fixed a minor bug with string %
|
4
6
|
2007-12-12 1.1.2 * fixed a bug with format strings
|
5
7
|
* detect if ruby 1.8.6 is installed and use its' to_date function
|
data/Manifest.txt
CHANGED
data/README.md
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
# Ruby Units
|
2
|
+
|
3
|
+
Kevin C. Olbrich, Ph.D.
|
4
|
+
|
5
|
+
[Sciwerks.com](http://www.sciwerks.com)
|
6
|
+
|
7
|
+
Project page: [http://github.com/olbrich/ruby-units](http://github.com/olbrich/ruby-units)
|
8
|
+
|
9
|
+
## Introduction
|
10
|
+
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.
|
11
|
+
|
12
|
+
## Solution
|
13
|
+
The 'Ruby units' gem is designed so 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
|
+
## Installation:
|
16
|
+
This package may be installed using: `gem install ruby-units`
|
17
|
+
|
18
|
+
## Usage:
|
19
|
+
unit = Unit.new("1") # constant only
|
20
|
+
unit = Unit.new("mm") # unit only (defaults to a value of 1)
|
21
|
+
unit = Unit.new("1 mm") # create a simple unit
|
22
|
+
unit = Unit.new("1 mm/s") # a compound unit
|
23
|
+
unit = Unit.new("1 mm s^-1") # in exponent notation
|
24
|
+
unit = Unit.new("1 kg*m^2/s^2") # complex unit
|
25
|
+
unit = Unit.new("1 kg m^2 s^-2") # complex unit
|
26
|
+
unit = Unit("1 mm") # shorthand
|
27
|
+
unit = "1 mm".to_unit # convert string object
|
28
|
+
unit = object.to_unit # convert any object using object.to_s
|
29
|
+
unit = U'1 mm'
|
30
|
+
unit = u'1 mm'
|
31
|
+
unit = '1 mm'.unit
|
32
|
+
unit = '1 mm'.u
|
33
|
+
unit = '1/4 cup'.unit # Rational number
|
34
|
+
unit = '1+1i mm'.unit # Complex Number
|
35
|
+
|
36
|
+
## Rules:
|
37
|
+
1. only 1 quantity per unit (with 2 exceptions... 6'5" and '8 lbs 8 oz')
|
38
|
+
2. use SI notation when possible
|
39
|
+
3. avoid using spaces in unit names
|
40
|
+
|
41
|
+
## Unit compatability:
|
42
|
+
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'.
|
43
|
+
|
44
|
+
unit1 =~ unit2 #=> true if units are compatible
|
45
|
+
|
46
|
+
## Unit Math:
|
47
|
+
Unit#+() # Add. only works if units are compatible
|
48
|
+
Unit#-() # Subtract. only works if units are compatible
|
49
|
+
Unit#*() # Multiply.
|
50
|
+
Unit#/() # Divide.
|
51
|
+
Unit#**() # Exponentiate. Exponent must be an integer, can be positive, negative, or zero
|
52
|
+
Unit#inverse # Returns 1/unit
|
53
|
+
Unit#abs # Returns absolute value of the unit quantity. Strips off the units
|
54
|
+
Unit#ceil # rounds quantity to next highest integer
|
55
|
+
Unit#floor # rounds quantity down to next lower integer
|
56
|
+
Unit#round # rounds quantity to nearest integer
|
57
|
+
Unit#to_int # returns the quantity as an integer
|
58
|
+
|
59
|
+
Unit will coerce other objects into a Unit if used in a formula. This means that ..
|
60
|
+
|
61
|
+
Unit("1 mm") + "2 mm" == Unit("3 mm")
|
62
|
+
|
63
|
+
This will work as expected so long as you start the formula with a Unit object.
|
64
|
+
|
65
|
+
## Conversions & comparisons
|
66
|
+
Units can be converted to other units in a couple of ways.
|
67
|
+
|
68
|
+
unit1 = unit >> "ft" # convert to 'feet'
|
69
|
+
unit >>= "ft" # convert and overwrite original object
|
70
|
+
unit3 = unit1 + unit2 # resulting object will have the units of unit1
|
71
|
+
unit3 = unit1 - unit2 # resulting object will have the units of unit1
|
72
|
+
unit1 <=> unit2 # does comparison on quantities in base units, throws an exception if not compatible
|
73
|
+
unit1 === unit2 # true if units and quantity are the same, even if 'equivalent' by <=>
|
74
|
+
unit.to('ft') # convert
|
75
|
+
unit1 + unit2 >> "ft" # converts result of math to 'ft'
|
76
|
+
(unit1 + unit2).to('ft') # converts result to 'ft'
|
77
|
+
|
78
|
+
Any object that defines a 'to_unit' method will be automatically coerced to a unit during calculations.
|
79
|
+
|
80
|
+
## Text Output
|
81
|
+
Units will display themselves nicely based on the preferred abbreviation for the units and prefixes.
|
82
|
+
Since Unit implements a Unit#to_s, all that is needed in most cases is:
|
83
|
+
|
84
|
+
"#{Unit.new('1 mm')}" #=> "1 mm"
|
85
|
+
|
86
|
+
The to_s also accepts some options.
|
87
|
+
|
88
|
+
Unit.new('1.5 mm').to_s("%0.2f") # "1.50 mm". Enter any valid format
|
89
|
+
string. Also accepts strftime format
|
90
|
+
U('1.5 mm').to_s("in") # converts to inches before printing
|
91
|
+
U("2 m").to_s(:ft) # returns 6'7"
|
92
|
+
U("100 kg").to_s(:lbs) # returns 220 lbs, 7 oz
|
93
|
+
|
94
|
+
|
95
|
+
## Time Helpers
|
96
|
+
Time, Date, and DateTime objects can have time units added or subtracted.
|
97
|
+
|
98
|
+
Time.now + "10 min".unit
|
99
|
+
|
100
|
+
Several helpers have also been defined.
|
101
|
+
Note: If you include the 'Chronic' gem, you can specify times in natural
|
102
|
+
language.
|
103
|
+
|
104
|
+
'min'.since('9/18/06 3:00pm')
|
105
|
+
'min'.before('9/18/08 3:00pm')
|
106
|
+
'days'.until('1/1/07')
|
107
|
+
'5 min'.from(Time.now)
|
108
|
+
'5 min'.from_now
|
109
|
+
'5 min'.before_now
|
110
|
+
'5 min'.before(Time.now)
|
111
|
+
'10 min'.ago
|
112
|
+
|
113
|
+
Durations may be entered as 'HH:MM:SS, usec' and will be returned in 'hours'.
|
114
|
+
|
115
|
+
'1:00'.unit #=> 1 h
|
116
|
+
'0:30'.unit #=> 0.5 h
|
117
|
+
'0:30:30'.unit #=> 0.5 h + 30 sec
|
118
|
+
|
119
|
+
If only one ":" is present, it is interpreted as the separator between hours and minutes.
|
120
|
+
|
121
|
+
## Ranges
|
122
|
+
[U('0 h')..U('10 h')].each {|x| p x}
|
123
|
+
works so long as the starting point has an integer scalar
|
124
|
+
|
125
|
+
## Math functions
|
126
|
+
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.
|
127
|
+
|
128
|
+
## Temperatures
|
129
|
+
Ruby-units makes a distinction between a temperature (which technically is a property) and degrees of temperature (which temperatures are measured in).
|
130
|
+
|
131
|
+
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'.unit) units behave like most other units.
|
132
|
+
|
133
|
+
'37 tempC'.unit >> 'tempF' #=> 98.6 tempF
|
134
|
+
|
135
|
+
Ruby-units will raise an exception if you attempt to create a temperature unit that would fall below absolute zero.
|
136
|
+
|
137
|
+
Unit math on temperatures is fairly limited.
|
138
|
+
|
139
|
+
'100 tempC'.unit + '10 degC'.unit # '110 tempC'.unit
|
140
|
+
'100 tempC'.unit - '10 degC'.unit # '90 tempC'.unit
|
141
|
+
'100 tempC'.unit + '50 tempC'.unit # exception
|
142
|
+
'100 tempC'.unit - '50 tempC'.unit # '50 degC'.unit
|
143
|
+
'50 tempC'.unit - '100 tempC'.unit # '-50 degC'.unit
|
144
|
+
'100 tempC'.unit * [scalar] # '100*scalar tempC'.unit
|
145
|
+
'100 tempC'.unit / [scalar] # '100/scalar tempC'.unit
|
146
|
+
'100 tempC'.unit * [unit] # exception
|
147
|
+
'100 tempC'.unit / [unit] # exception
|
148
|
+
'100 tempC'.unit ** N # exception
|
149
|
+
|
150
|
+
'100 tempC'.unit >> 'degC' #=> '100 degC'.unit
|
151
|
+
This conversion references the 0 point on the scale of the temperature unit
|
152
|
+
|
153
|
+
'100 degC'.unit >> 'tempC' #=> '-173 tempC'.unit
|
154
|
+
These conversions are always interpreted as being relative to absolute zero.
|
155
|
+
Conversions are probably better done like this...
|
156
|
+
|
157
|
+
'0 tempC'.unit + '100 degC'.unit #=> '100 tempC'.unit
|
158
|
+
|
data/Rakefile
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'hoe'
|
3
|
+
|
4
|
+
Hoe.plugin :yard
|
5
|
+
|
3
6
|
require './lib/ruby_units/units'
|
4
7
|
require './lib/ruby_units/ruby-units'
|
5
8
|
|
@@ -13,11 +16,13 @@ rescue
|
|
13
16
|
end
|
14
17
|
|
15
18
|
Hoe.spec('ruby-units') do |p|
|
19
|
+
p.yard_title = "Ruby-Units"
|
20
|
+
p.yard_markup = "markdown"
|
16
21
|
p.version = Unit::VERSION
|
17
22
|
p.rubyforge_name = 'ruby-units'
|
18
23
|
p.summary = %q{A class that performs unit conversions and unit math}
|
19
24
|
p.email = 'kevin.olbrich+ruby_units@gmail.com'
|
20
|
-
p.url = 'http://
|
25
|
+
p.url = 'http://github.com/olbrich/ruby-units'
|
21
26
|
p.description = "This library handles unit conversions and unit math"
|
22
27
|
p.changes = p.paragraphs_of('CHANGELOG.txt', 0..1).join("\n\n")
|
23
28
|
p.author = 'Kevin Olbrich, Ph.D'
|
@@ -40,7 +40,7 @@ require 'parsedate'
|
|
40
40
|
# Unit.setup
|
41
41
|
class Unit < Numeric
|
42
42
|
# pre-generate hashes from unit definitions for performance.
|
43
|
-
VERSION = '1.1.
|
43
|
+
VERSION = '1.1.5'
|
44
44
|
@@USER_DEFINITIONS = {}
|
45
45
|
@@PREFIX_VALUES = {}
|
46
46
|
@@PREFIX_MAP = {}
|
@@ -132,8 +132,8 @@ class Unit < Numeric
|
|
132
132
|
end
|
133
133
|
@@OUTPUT_MAP[key]=value[0][0]
|
134
134
|
end
|
135
|
-
@@PREFIX_REGEX = @@PREFIX_MAP.keys.sort_by {|prefix| prefix.length}.reverse.join('|')
|
136
|
-
@@UNIT_REGEX = @@UNIT_MAP.keys.sort_by {|unit| unit.length}.reverse.join('|')
|
135
|
+
@@PREFIX_REGEX = @@PREFIX_MAP.keys.sort_by {|prefix| [prefix.length, prefix]}.reverse.join('|')
|
136
|
+
@@UNIT_REGEX = @@UNIT_MAP.keys.sort_by {|unit| [unit.length, unit]}.reverse.join('|')
|
137
137
|
@@UNIT_MATCH_REGEX = /(#{@@PREFIX_REGEX})*?(#{@@UNIT_REGEX})\b/
|
138
138
|
Unit.new(1)
|
139
139
|
end
|
@@ -372,11 +372,11 @@ class Unit < Numeric
|
|
372
372
|
"#{$1 % @scalar} #{$2 || self.units}".strip
|
373
373
|
end
|
374
374
|
rescue
|
375
|
-
(
|
375
|
+
(DateTime.new(0) + self).strftime(target_units)
|
376
376
|
end
|
377
|
-
when /(\S+)/
|
377
|
+
when /(\S+)/ #unit only 'mm' or '1/mm'
|
378
378
|
"#{self.to($1).to_s}"
|
379
|
-
else
|
379
|
+
else
|
380
380
|
raise "unhandled case"
|
381
381
|
end
|
382
382
|
else
|
@@ -1044,7 +1044,7 @@ class Unit < Numeric
|
|
1044
1044
|
@base_scalar *= mult
|
1045
1045
|
return self
|
1046
1046
|
end
|
1047
|
-
|
1047
|
+
unit_string.gsub!(/<(#{@@UNIT_REGEX})><(#{@@UNIT_REGEX})>/, '\1*\2')
|
1048
1048
|
unit_string.gsub!(/[<>]/,"")
|
1049
1049
|
|
1050
1050
|
if unit_string =~ /:/
|
@@ -1079,7 +1079,6 @@ class Unit < Numeric
|
|
1079
1079
|
raise( ArgumentError, "'#{passed_unit_string}' Unit not recognized") if unit_string.count('/') > 1
|
1080
1080
|
raise( ArgumentError, "'#{passed_unit_string}' Unit not recognized") if unit_string.scan(/\s[02-9]/).size > 0
|
1081
1081
|
@scalar, top, bottom = unit_string.scan(UNIT_STRING_REGEX)[0] #parse the string into parts
|
1082
|
-
|
1083
1082
|
top.scan(TOP_REGEX).each do |item|
|
1084
1083
|
n = item[1].to_i
|
1085
1084
|
x = "#{item[0]} "
|
@@ -1096,8 +1095,10 @@ class Unit < Numeric
|
|
1096
1095
|
@denominator ||= UNITY_ARRAY
|
1097
1096
|
@numerator = top.scan(@@UNIT_MATCH_REGEX).delete_if {|x| x.empty?}.compact if top
|
1098
1097
|
@denominator = bottom.scan(@@UNIT_MATCH_REGEX).delete_if {|x| x.empty?}.compact if bottom
|
1098
|
+
|
1099
|
+
|
1099
1100
|
us = "#{(top || '' + bottom || '')}".to_s.gsub(@@UNIT_MATCH_REGEX,'').gsub(/[\d\*, "'_^\/\$]/,'')
|
1100
|
-
raise( ArgumentError, "'#{passed_unit_string}' Unit not recognized") unless us.empty?
|
1101
|
+
raise( ArgumentError, "'#{passed_unit_string}' Unit not recognized #{us.inspect}") unless us.empty?
|
1101
1102
|
|
1102
1103
|
@numerator = @numerator.map do |item|
|
1103
1104
|
@@PREFIX_MAP[item[0]] ? [@@PREFIX_MAP[item[0]], @@UNIT_MAP[item[1]]] : [@@UNIT_MAP[item[1]]]
|
data/test/test_ruby-units.rb
CHANGED
@@ -527,7 +527,8 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
527
527
|
unit1 = Unit.new("1.1 mm")
|
528
528
|
unit2 = Unit.new("2 mm")
|
529
529
|
assert_equal unit2, unit1.ceil
|
530
|
-
assert_equal(('1 mm'.unit / '1 mm'.unit).ceil, 1)
|
530
|
+
assert_equal(('1 mm'.unit / '1 mm'.unit).ceil, 1)
|
531
|
+
assert_equal("11 kg*m".unit, ("1003 kg*m".unit / 100).ceil)
|
531
532
|
end
|
532
533
|
|
533
534
|
def test_floor
|
@@ -756,7 +757,7 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
756
757
|
a = '1 +/- 1 mm'.unit
|
757
758
|
assert_equal a.to_s, '1 +/- 1 mm'
|
758
759
|
else
|
759
|
-
|
760
|
+
warn "Can't test Uncertain Units unless 'Uncertain' gem is installed"
|
760
761
|
end
|
761
762
|
end
|
762
763
|
|
@@ -893,7 +894,8 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
893
894
|
|
894
895
|
def test_explicit_init
|
895
896
|
assert_equal '1 lbf'.unit, '1 <pound-force>'.unit
|
896
|
-
assert_equal '1 lbs'.unit, '1 <pound>'.unit
|
897
|
+
assert_equal '1 lbs'.unit, '1 <pound>'.unit
|
898
|
+
assert_equal('1 kg*m'.unit, '1 <kilogram>*<meter>'.unit)
|
897
899
|
end
|
898
900
|
|
899
901
|
def test_format_nil_string
|
@@ -912,7 +914,13 @@ class TestRubyUnits < Test::Unit::TestCase
|
|
912
914
|
end
|
913
915
|
|
914
916
|
def test_version
|
915
|
-
assert_equal('1.1.
|
917
|
+
assert_equal('1.1.5', Unit::VERSION)
|
916
918
|
end
|
919
|
+
|
920
|
+
def test_negation
|
921
|
+
a = 1.to_unit
|
922
|
+
assert_equal(a.class, (1-a).class)
|
923
|
+
end
|
924
|
+
|
917
925
|
end
|
918
926
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 1
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 1.1.
|
8
|
+
- 5
|
9
|
+
version: 1.1.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kevin Olbrich, Ph.D
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-03-
|
17
|
+
date: 2010-03-17 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -40,15 +40,29 @@ dependencies:
|
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
segments:
|
42
42
|
- 0
|
43
|
-
-
|
44
|
-
-
|
45
|
-
version: 0.
|
43
|
+
- 5
|
44
|
+
- 0
|
45
|
+
version: 0.5.0
|
46
46
|
type: :development
|
47
47
|
version_requirements: *id002
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
|
-
name: hoe
|
49
|
+
name: hoe-yard
|
50
50
|
prerelease: false
|
51
51
|
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
- 1
|
58
|
+
- 2
|
59
|
+
version: 0.1.2
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: hoe
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
66
|
requirements:
|
53
67
|
- - ">="
|
54
68
|
- !ruby/object:Gem::Version
|
@@ -58,7 +72,7 @@ dependencies:
|
|
58
72
|
- 0
|
59
73
|
version: 2.5.0
|
60
74
|
type: :development
|
61
|
-
version_requirements: *
|
75
|
+
version_requirements: *id004
|
62
76
|
description: This library handles unit conversions and unit math
|
63
77
|
email: kevin.olbrich+ruby_units@gmail.com
|
64
78
|
executables: []
|
@@ -68,12 +82,11 @@ extensions: []
|
|
68
82
|
extra_rdoc_files:
|
69
83
|
- CHANGELOG.txt
|
70
84
|
- Manifest.txt
|
71
|
-
- README.txt
|
72
85
|
- LICENSE.txt
|
73
86
|
files:
|
74
87
|
- CHANGELOG.txt
|
75
88
|
- Manifest.txt
|
76
|
-
- README.
|
89
|
+
- README.md
|
77
90
|
- LICENSE.txt
|
78
91
|
- Rakefile
|
79
92
|
- lib/ruby-units.rb
|
@@ -89,14 +102,17 @@ files:
|
|
89
102
|
- lib/ruby_units/complex.rb
|
90
103
|
- lib/ruby_units/ruby-units.rb
|
91
104
|
- test/test_ruby-units.rb
|
92
|
-
has_rdoc:
|
93
|
-
homepage: http://
|
105
|
+
has_rdoc: yard
|
106
|
+
homepage: http://github.com/olbrich/ruby-units
|
94
107
|
licenses: []
|
95
108
|
|
96
109
|
post_install_message:
|
97
110
|
rdoc_options:
|
98
|
-
- --
|
99
|
-
-
|
111
|
+
- --title
|
112
|
+
- Ruby-Units
|
113
|
+
- --markup
|
114
|
+
- markdown
|
115
|
+
- --quiet
|
100
116
|
require_paths:
|
101
117
|
- lib
|
102
118
|
required_ruby_version: !ruby/object:Gem::Requirement
|
data/README.txt
DELETED
@@ -1,174 +0,0 @@
|
|
1
|
-
= Ruby Units
|
2
|
-
|
3
|
-
Kevin C. Olbrich, Ph.D.
|
4
|
-
|
5
|
-
kevin.olbrich@gmail.com
|
6
|
-
|
7
|
-
http://www.sciwerks.com
|
8
|
-
|
9
|
-
Project page: http://ruby-units.rubyforge.org/ruby-units
|
10
|
-
|
11
|
-
== Introduction
|
12
|
-
Many technical applications make use of specialized calculations at some point.
|
13
|
-
Frequently, these calculations require unit conversions to ensure accurate results.
|
14
|
-
Needless to say, this is a pain to properly keep track of, and is prone to numerous errors.
|
15
|
-
|
16
|
-
== Solution
|
17
|
-
The 'Ruby units' gem is designed so simplify the handling of units for scientific calculations.
|
18
|
-
The units of each quantity are specified when a Unit object is created and the Unit class will
|
19
|
-
handle all subsequent conversions and manipulations to ensure an accurate result.
|
20
|
-
|
21
|
-
== Installation:
|
22
|
-
This package may be installed using:
|
23
|
-
gem install ruby-units
|
24
|
-
|
25
|
-
== Usage:
|
26
|
-
unit = Unit.new("1") # constant only
|
27
|
-
unit = Unit.new("mm") # unit only (defaults to a value of 1)
|
28
|
-
unit = Unit.new("1 mm") # create a simple unit
|
29
|
-
unit = Unit.new("1 mm/s") # a compound unit
|
30
|
-
unit = Unit.new("1 mm s^-1") # in exponent notation
|
31
|
-
unit = Unit.new("1 kg*m^2/s^2") # complex unit
|
32
|
-
unit = Unit.new("1 kg m^2 s^-2") # complex unit
|
33
|
-
unit = Unit("1 mm") # shorthand
|
34
|
-
unit = "1 mm".to_unit # convert string object
|
35
|
-
unit = object.to_unit # convert any object using object.to_s
|
36
|
-
unit = U'1 mm'
|
37
|
-
unit = u'1 mm'
|
38
|
-
unit = '1 mm'.unit
|
39
|
-
unit = '1 mm'.u
|
40
|
-
unit = '1/4 cup'.unit # Rational number
|
41
|
-
unit = '1+1i mm'.unit # Complex Number
|
42
|
-
|
43
|
-
== Rules:
|
44
|
-
1. only 1 quantity per unit (with 2 exceptions... 6'5" and '8 lbs 8 oz')
|
45
|
-
2. use SI notation when possible
|
46
|
-
3. avoid using spaces in unit names
|
47
|
-
|
48
|
-
== Unit compatability:
|
49
|
-
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'.
|
50
|
-
|
51
|
-
unit1 =~ unit2 #=> true if units are compatible
|
52
|
-
|
53
|
-
== Unit Math:
|
54
|
-
|
55
|
-
<b>Method</b>:: <b>Comment</b>
|
56
|
-
Unit#+():: Add. only works if units are compatible
|
57
|
-
Unit#-():: Subtract. only works if units are compatible
|
58
|
-
Unit#*():: Multiply.
|
59
|
-
Unit#/():: Divide.
|
60
|
-
Unit#**():: Exponentiate. Exponent must be an integer, can be positive, negative, or zero
|
61
|
-
Unit#inverse:: Returns 1/unit
|
62
|
-
Unit#abs:: Returns absolute value of the unit quantity. Strips off the units
|
63
|
-
Unit#ceil:: rounds quantity to next highest integer
|
64
|
-
Unit#floor:: rounds quantity down to next lower integer
|
65
|
-
Unit#round:: rounds quantity to nearest integer
|
66
|
-
Unit#to_int:: returns the quantity as an integer
|
67
|
-
|
68
|
-
Unit will coerce other objects into a Unit if used in a formula. This means that ..
|
69
|
-
|
70
|
-
Unit("1 mm") + "2 mm" == Unit("3 mm")
|
71
|
-
|
72
|
-
This will work as expected so long as you start the formula with a Unit object.
|
73
|
-
|
74
|
-
== Conversions & comparisons
|
75
|
-
|
76
|
-
Units can be converted to other units in a couple of ways.
|
77
|
-
|
78
|
-
unit1 = unit >> "ft" # => convert to 'feet'
|
79
|
-
unit >>= "ft" # => convert and overwrite original object
|
80
|
-
unit3 = unit1 + unit2 # => resulting object will have the units of unit1
|
81
|
-
unit3 = unit1 - unit2 # => resulting object will have the units of unit1
|
82
|
-
unit1 <=> unit2 # => does comparison on quantities in base units,
|
83
|
-
throws an exception if not compatible
|
84
|
-
unit1 === unit2 # => true if units and quantity are the same, even if
|
85
|
-
'equivalent' by <=>
|
86
|
-
unit.to('ft') # convert
|
87
|
-
unit1 + unit2 >> "ft" # converts result of math to 'ft'
|
88
|
-
(unit1 + unit2).to('ft') # converts result to 'ft'
|
89
|
-
|
90
|
-
Any object that defines a 'to_unit' method will be automatically coerced to a unit during calculations.
|
91
|
-
|
92
|
-
== Text Output
|
93
|
-
Units will display themselves nicely based on the preferred abbreviation for the units and prefixes.
|
94
|
-
Since Unit implements a Unit#to_s, all that is needed in most cases is:
|
95
|
-
"#{Unit.new('1 mm')}" #=> "1 mm"
|
96
|
-
|
97
|
-
The to_s also accepts some options.
|
98
|
-
Unit.new('1.5 mm').to_s("%0.2f") # => "1.50 mm". Enter any valid format
|
99
|
-
string. Also accepts strftime format
|
100
|
-
U('1.5 mm').to_s("in") # => converts to inches before printing
|
101
|
-
U("2 m").to_s(:ft) #=> returns 6'7"
|
102
|
-
U("100 kg").to_s(:lbs) #=> returns 220 lbs, 7 oz
|
103
|
-
|
104
|
-
|
105
|
-
== Time Helpers
|
106
|
-
|
107
|
-
Time, Date, and DateTime objects can have time units added or subtracted.
|
108
|
-
|
109
|
-
Time.now + "10 min".unit
|
110
|
-
|
111
|
-
Several helpers have also been defined.
|
112
|
-
Note: If you include the 'Chronic' gem, you can specify times in natural
|
113
|
-
language.
|
114
|
-
|
115
|
-
'min'.since('9/18/06 3:00pm')
|
116
|
-
'min'.before('9/18/08 3:00pm')
|
117
|
-
'days'.until('1/1/07')
|
118
|
-
'5 min'.from(Time.now)
|
119
|
-
'5 min'.from_now
|
120
|
-
'5 min'.before_now
|
121
|
-
'5 min'.before(Time.now)
|
122
|
-
'10 min'.ago
|
123
|
-
|
124
|
-
Durations may be entered as 'HH:MM:SS, usec' and will be returned in 'hours'.
|
125
|
-
|
126
|
-
'1:00'.unit #=> 1 h
|
127
|
-
'0:30'.unit #=> 0.5 h
|
128
|
-
'0:30:30'.unit #=> 0.5 h + 30 sec
|
129
|
-
|
130
|
-
If only one ":" is present, it is interpreted as the separator between hours and minutes.
|
131
|
-
|
132
|
-
== Ranges
|
133
|
-
|
134
|
-
[U('0 h')..U('10 h')].each {|x| p x}
|
135
|
-
works so long as the starting point has an integer scalar
|
136
|
-
|
137
|
-
== Math functions
|
138
|
-
All Trig math functions (sin, cos, sinh, hypot...) can take a unit as their parameter.
|
139
|
-
It will be converted to radians and then used if possible.
|
140
|
-
|
141
|
-
== Temperatures
|
142
|
-
Ruby-units makes a distinction between a temperature (which technically is a property) and
|
143
|
-
degrees of temperature (which temperatures are measured in).
|
144
|
-
|
145
|
-
Temperature units (i.e., 'tempK') can be converted back and forth, and will take into account
|
146
|
-
the differences in the zero points of the various scales. Differential temperature (e.g., '100 degC'.unit)
|
147
|
-
units behave like most other units.
|
148
|
-
|
149
|
-
'37 tempC'.unit >> 'tempF' #=> 98.6 tempF
|
150
|
-
|
151
|
-
Ruby-units will raise an exception if you attempt to create a temperature unit that would
|
152
|
-
fall below absolute zero.
|
153
|
-
|
154
|
-
Unit math on temperatures is fairly limited.
|
155
|
-
|
156
|
-
'100 tempC'.unit + '10 degC'.unit #=> '110 tempC'.unit
|
157
|
-
'100 tempC'.unit - '10 degC'.unit #=> '90 tempC'.unit
|
158
|
-
'100 tempC'.unit + '50 tempC'.unit #=> exception
|
159
|
-
'100 tempC'.unit - '50 tempC'.unit #=> '50 degC'.unit
|
160
|
-
'50 tempC'.unit - '100 tempC'.unit #=> '-50 degC'.unit
|
161
|
-
'100 tempC'.unit * [scalar] #=> '100*scalar tempC'.unit
|
162
|
-
'100 tempC'.unit / [scalar] #=> '100/scalar tempC'.unit
|
163
|
-
'100 tempC'.unit * [unit] #=> exception
|
164
|
-
'100 tempC'.unit / [unit] #=> exception
|
165
|
-
'100 tempC'.unit ** N #=> exception
|
166
|
-
|
167
|
-
'100 tempC'.unit >> 'degC' #=> '100 degC'.unit
|
168
|
-
This conversion references the 0 point on the scale of the temperature unit
|
169
|
-
|
170
|
-
'100 degC'.unit >> 'tempC' #=> '-173 tempC'.unit
|
171
|
-
These conversions are always interpreted as being relative to absolute zero.
|
172
|
-
Conversions are probably better done like this...
|
173
|
-
'0 tempC'.unit + '100 degC'.unit #=> '100 tempC'.unit
|
174
|
-
|