ruby-units-brewpoo 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,204 @@
1
+ Change Log for Ruby-units
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
5
+ 2007-12-13 1.1.3 * fixed a minor bug with string %
6
+ 2007-12-12 1.1.2 * fixed a bug with format strings
7
+ * detect if ruby 1.8.6 is installed and use its' to_date function
8
+
9
+ 2007-07-14 1.1.1 * fixed bug that would prevent creating '<pound-mass>' units, which
10
+ prevented rounding from working
11
+ * tests do not fail if Uncertain gem is not installed, you just get an
12
+ annoying warning message
13
+
14
+ 2007-01-28 1.1.0 * completely revamped the temperature handling system (see README)
15
+ * fixed some spelling errors in some units
16
+ * fixed to_datetime and to_date to convert durations to datetimes and dates'
17
+
18
+ 2007-01-24 1.0.2 * Minor changes in the way powers are calculated to support Uncertain
19
+ numbers better.
20
+ * Fixed parsing bug with Uncertain Numbers
21
+ * added resolution / typography units (pixels, points, pica)
22
+ Note that 'pt' means 'pints' and not 'points'
23
+ * added some pressure units ('inHg' & 'inH2O')
24
+ * changed default abbreviation of 'knots' to 'kt'
25
+ * Changed directory layout
26
+ * fixed a minor bug with Time.to_date so comparisons work properly
27
+
28
+ 2007-01-17 1.0.1 * Force units are now defined correctly.
29
+
30
+ 2007-01-12 1.0.0 * Improved handling of complex numbers. Now you can specify
31
+ '1+1i mm'.unit to get a complex unit.
32
+ * Taking the root of a negative unit will give you a complex unit
33
+ * fixed unary minus to work again
34
+ * Math.hypot now takes units. Both parameters must be the compatible
35
+ units or it will assert. Units will be converted to a common base
36
+ before use.
37
+ * Can now specify units in rational numbers, i.e., '1/4 cup'.unit
38
+ * Seems like a good time to move to 1.0 status
39
+
40
+ 2006-12-15 0.3.9 * forgot to increment the version in the gem file..ooops.
41
+
42
+ 2006-12-15 0.3.8 * Any object that supports a 'to_unit' method will now be
43
+ automatically coerced to a unit during math operations.
44
+
45
+ 2006-12-14 0.3.7 * improved handling of percents and added a 'wt%' unit
46
+ equivalent to 1 g/dl.
47
+ * Improved handling for units with non-alphanumeric names
48
+ (like ' for feet, # for pound)
49
+ * Now you can enter durations as "HH:MM:SS, usec" or
50
+ "HH:MM:SS:usec"
51
+
52
+ 2006-12-05 0.3.6 * Fixed bug where (unit/unit).ceil would fail
53
+
54
+ 2006-11-20 0.3.5 * Minor bug fixes
55
+ * to_int now coerces the result to an actual Integer,
56
+ but only works properly for unitless Units.
57
+
58
+ 2006-10-27 0.3.4 * Fixed a few more parsing bugs so that it will properly
59
+ complain about malformed units.
60
+ * Fixed a bug that prevents proper use of percents
61
+ * several minor tweaks
62
+ * some improved Date and DateTime handling
63
+ * can convert between Date, DateTime, and Time objects
64
+ * Time math will now return a DateTime if it goes out of
65
+ range.
66
+
67
+ 2006-10-03 0.3.3 * Apparently I can't do math late at night.
68
+ Fixed a bug that would cause problems when adding
69
+ or subtracting units to a unit with a zero scalar.
70
+ * Date and DateTime objects can be converted to 'units'
71
+
72
+ 2006-10-03 0.3.2 * More minor bug fixes
73
+ (now fixes a minor name collision with rails)
74
+
75
+ 2006-10-02 0.3.1 * minor bug fixes
76
+
77
+ 2006-10-02 0.3.0 * Performance enhanced by caching results of many
78
+ functions (Thanks to Kurt Stephens for pushing this.)
79
+ * Throws an exception if the unit is not recognized
80
+ * units can now identify what 'kind' they are
81
+ (:length, :mass, etc..)
82
+ * New constructors:
83
+ Unit(1,"mm")
84
+ Unit(1,"mm/s")
85
+ Unit(1,"mm","s")
86
+
87
+ 2006-09-22 0.2.3 * added support for date/time parsing with the Chronic gem
88
+ parsing will use Chronic if it is loaded
89
+ * allows Date / Time / DateTime conversions
90
+ * better test coverage
91
+ * The 'string'.to_time returns a Time object
92
+ * 'string'.to_datetime returns a DateTime object
93
+ * 'string'.time returns a Time object or a DateTime if the
94
+ Time object fails
95
+ * 'string'.datetime returns a DateTime or a Time if the
96
+ DateTime fails
97
+
98
+ 2006-09-19 0.2.2 * tweaked temperature handling a bit. Now enter
99
+ temperatures like this:
100
+ '0 tempC'.unit #=> 273.15 degK
101
+ They will always be converted to kelvin to avoid
102
+ problems when temperatures are used in equations.
103
+ * added Time.in("5 min")
104
+ * added Unit.to_unit to simplify some calls
105
+
106
+ 2006-09-18 0.2.1 * Trig math functions (sin, cos, tan, sinh, cosh, tanh)
107
+ accept units that can be converted to radians
108
+ Math.sin("90 deg".unit) => 1.0
109
+ * Date and DateTime can be offset by a time unit
110
+ (Date.today + "1 day".unit) => 2006-09-19
111
+ Does not work with months since they aren't a consistent
112
+ size
113
+ * Tweaked time usage a bit
114
+ Time.now + "1 hr".unit => Mon Sep 18 11:51:29 EDT 2006
115
+ * can output time in 'hh:mm:ss' format by using
116
+ 'unit.to_s(:time)'
117
+ * added time helper methods
118
+ ago,
119
+ since(Time/DateTime),
120
+ until(Time/DateTime),
121
+ from(Time/DateTime),
122
+ before(Time/DateTime), and
123
+ after(Time/DateTime)
124
+ * Time helpers also work on strings. In this case they
125
+ are first converted to units
126
+ '5 min'.from_now
127
+ '1 week'.ago
128
+ 'min'.since(time)
129
+ 'min'.until(time)
130
+ '1 day'.from()
131
+ * Can pass Strings to time helpers and they will be parsed
132
+ with ParseDate
133
+ * Fixed most parsing bugs (I think)
134
+ * Can pass a strftime format string to to_s to format time
135
+ output
136
+ * can use U'1 mm' or '1 mm'.u to specify units now
137
+
138
+ 2006-09-17 * can now use the '%' format specifier like
139
+ '%0.2f' % '1 mm'.unit #=> '1.00 mm'
140
+ * works nicely with time now.
141
+ '1 week'.unit + Time.now => 1.159e+09 s
142
+ Time.at('1.159e+09 s'.unit)
143
+ => Sat Sep 23 04:26:40 EDT 2006
144
+ "1.159e9 s".unit.time
145
+ => Sat Sep 23 04:26:40 EDT 2006
146
+ * Time.now.unit => 1.159e9 s
147
+ * works well with 'Uncertain' numerics
148
+ (www.rubyforge.org/projects/uncertain)
149
+ * Improved parsing
150
+
151
+ 2006-08-28 0.2.0 * Added 'ruby_unit.rb' file so that requires will still
152
+ work if the wrong name is used
153
+ * Added 'to' as an alias to '>>' so conversions can be
154
+ done as '1 m'.unit.to('1 cm')
155
+ * Added ability to convert temperatures to absolute values
156
+ using the following syntax:
157
+ '37 degC'.unit.to('tempF') #=> '98.6 degF'.unit
158
+ * Tweaked abbreviations a bit. 'ton' is now 'tn' instead
159
+ of 't'. It was causing parse collisions with 'atm'.
160
+ * fixed a bug in term elimination routine
161
+ * fixed a bug in parsing of powers, and added support for
162
+ 'm**2' format
163
+ * Added support for taking roots of units. Just
164
+ exponentiate with a fraction (0.5, 1.0/3, 0.25)
165
+ * renamed 'quantity' to 'scalar'
166
+ * any type of Numeric can be used to initialize a Unit,
167
+ although this can't really be done with a string
168
+ * Units can not be forced to a float using to_f unless
169
+ they are unitless. This prevents some math functions
170
+ from forcing the conversion. To get the scalar, just
171
+ use 'unit.scalar'
172
+ * 'inspect' returns string representation
173
+ * better edge-case detection with math functions.
174
+ "0 mm".unit**-1 now throws a ZeroDivisionError exception
175
+ * Ranges can make a series of units, so long as the end
176
+ points have integer scalars.
177
+ * Fixed a parsing bug with feet/pounds and scientific
178
+ numbers
179
+
180
+ 2006-08-22 0.1.1 * Added new format option "1 mm".to_unit("in") now
181
+ converts the result to the indicated units
182
+ * Fixed some naming issues so that the gem name matches
183
+ the require name.
184
+ * Added CHANGELOG
185
+ * Improved test coverage (100% code coverage via RCov)
186
+ * fixed a bug that prevented units with a prefix in the
187
+ denominator from converting properly
188
+ * can use .unit method on a string to create a new unit
189
+ object
190
+ * can now coerce or define units from arrays, strings,
191
+ numerics.
192
+ "1 mm".unit + [1, 'mm'] === "2 mm".unit
193
+ [1,'mm','s'].unit === "1 mm/s".unit
194
+ 2.5.unit === "2.5".unit
195
+ * Added instructions on how to add custom units
196
+
197
+ 2006-08-22 0.1.0 * Initial Release
198
+
199
+
200
+
201
+
202
+
203
+
204
+
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006-2007 Kevin C. Olbrich
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
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:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ CHANGELOG.txt
2
+ Manifest.txt
3
+ README.md
4
+ LICENSE.txt
5
+ Rakefile
6
+ lib/ruby-units.rb
7
+ lib/ruby_units.rb
8
+ lib/ruby_units/units.rb
9
+ lib/ruby_units/math.rb
10
+ lib/ruby_units/date.rb
11
+ lib/ruby_units/time.rb
12
+ lib/ruby_units/string.rb
13
+ lib/ruby_units/array.rb
14
+ lib/ruby_units/numeric.rb
15
+ lib/ruby_units/object.rb
16
+ lib/ruby_units/array.rb
17
+ lib/ruby_units/complex.rb
18
+ lib/ruby_units/ruby-units.rb
19
+ test/test_ruby-units.rb
@@ -0,0 +1,160 @@
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
+ Forked by [brewpoo] to add additional unit support
10
+
11
+ ## Introduction
12
+ 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.
13
+
14
+ ## Solution
15
+ 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.
16
+
17
+ ## Installation:
18
+ This package may be installed using: `gem install ruby-units`
19
+
20
+ ## Usage:
21
+ unit = Unit.new("1") # constant only
22
+ unit = Unit.new("mm") # unit only (defaults to a value of 1)
23
+ unit = Unit.new("1 mm") # create a simple unit
24
+ unit = Unit.new("1 mm/s") # a compound unit
25
+ unit = Unit.new("1 mm s^-1") # in exponent notation
26
+ unit = Unit.new("1 kg*m^2/s^2") # complex unit
27
+ unit = Unit.new("1 kg m^2 s^-2") # complex unit
28
+ unit = Unit("1 mm") # shorthand
29
+ unit = "1 mm".to_unit # convert string object
30
+ unit = object.to_unit # convert any object using object.to_s
31
+ unit = U'1 mm'
32
+ unit = u'1 mm'
33
+ unit = '1 mm'.unit
34
+ unit = '1 mm'.u
35
+ unit = '1/4 cup'.unit # Rational number
36
+ unit = '1+1i mm'.unit # Complex Number
37
+
38
+ ## Rules:
39
+ 1. only 1 quantity per unit (with 2 exceptions... 6'5" and '8 lbs 8 oz')
40
+ 2. use SI notation when possible
41
+ 3. avoid using spaces in unit names
42
+
43
+ ## Unit compatability:
44
+ 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'.
45
+
46
+ unit1 =~ unit2 #=> true if units are compatible
47
+
48
+ ## Unit Math:
49
+ Unit#+() # Add. only works if units are compatible
50
+ Unit#-() # Subtract. only works if units are compatible
51
+ Unit#*() # Multiply.
52
+ Unit#/() # Divide.
53
+ Unit#**() # Exponentiate. Exponent must be an integer, can be positive, negative, or zero
54
+ Unit#inverse # Returns 1/unit
55
+ Unit#abs # Returns absolute value of the unit quantity. Strips off the units
56
+ Unit#ceil # rounds quantity to next highest integer
57
+ Unit#floor # rounds quantity down to next lower integer
58
+ Unit#round # rounds quantity to nearest integer
59
+ Unit#to_int # returns the quantity as an integer
60
+
61
+ Unit will coerce other objects into a Unit if used in a formula. This means that ..
62
+
63
+ Unit("1 mm") + "2 mm" == Unit("3 mm")
64
+
65
+ This will work as expected so long as you start the formula with a Unit object.
66
+
67
+ ## Conversions & comparisons
68
+ Units can be converted to other units in a couple of ways.
69
+
70
+ unit1 = unit >> "ft" # convert to 'feet'
71
+ unit >>= "ft" # convert and overwrite original object
72
+ unit3 = unit1 + unit2 # resulting object will have the units of unit1
73
+ unit3 = unit1 - unit2 # resulting object will have the units of unit1
74
+ unit1 <=> unit2 # does comparison on quantities in base units, throws an exception if not compatible
75
+ unit1 === unit2 # true if units and quantity are the same, even if 'equivalent' by <=>
76
+ unit.to('ft') # convert
77
+ unit1 + unit2 >> "ft" # converts result of math to 'ft'
78
+ (unit1 + unit2).to('ft') # converts result to 'ft'
79
+
80
+ Any object that defines a 'to_unit' method will be automatically coerced to a unit during calculations.
81
+
82
+ ## Text Output
83
+ Units will display themselves nicely based on the preferred abbreviation for the units and prefixes.
84
+ Since Unit implements a Unit#to_s, all that is needed in most cases is:
85
+
86
+ "#{Unit.new('1 mm')}" #=> "1 mm"
87
+
88
+ The to_s also accepts some options.
89
+
90
+ Unit.new('1.5 mm').to_s("%0.2f") # "1.50 mm". Enter any valid format
91
+ string. Also accepts strftime format
92
+ U('1.5 mm').to_s("in") # converts to inches before printing
93
+ U("2 m").to_s(:ft) # returns 6'7"
94
+ U("100 kg").to_s(:lbs) # returns 220 lbs, 7 oz
95
+
96
+
97
+ ## Time Helpers
98
+ Time, Date, and DateTime objects can have time units added or subtracted.
99
+
100
+ Time.now + "10 min".unit
101
+
102
+ Several helpers have also been defined.
103
+ Note: If you include the 'Chronic' gem, you can specify times in natural
104
+ language.
105
+
106
+ 'min'.since('9/18/06 3:00pm')
107
+ 'min'.before('9/18/08 3:00pm')
108
+ 'days'.until('1/1/07')
109
+ '5 min'.from(Time.now)
110
+ '5 min'.from_now
111
+ '5 min'.before_now
112
+ '5 min'.before(Time.now)
113
+ '10 min'.ago
114
+
115
+ Durations may be entered as 'HH:MM:SS, usec' and will be returned in 'hours'.
116
+
117
+ '1:00'.unit #=> 1 h
118
+ '0:30'.unit #=> 0.5 h
119
+ '0:30:30'.unit #=> 0.5 h + 30 sec
120
+
121
+ If only one ":" is present, it is interpreted as the separator between hours and minutes.
122
+
123
+ ## Ranges
124
+ [U('0 h')..U('10 h')].each {|x| p x}
125
+ works so long as the starting point has an integer scalar
126
+
127
+ ## Math functions
128
+ 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.
129
+
130
+ ## Temperatures
131
+ Ruby-units makes a distinction between a temperature (which technically is a property) and degrees of temperature (which temperatures are measured in).
132
+
133
+ 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.
134
+
135
+ '37 tempC'.unit >> 'tempF' #=> 98.6 tempF
136
+
137
+ Ruby-units will raise an exception if you attempt to create a temperature unit that would fall below absolute zero.
138
+
139
+ Unit math on temperatures is fairly limited.
140
+
141
+ '100 tempC'.unit + '10 degC'.unit # '110 tempC'.unit
142
+ '100 tempC'.unit - '10 degC'.unit # '90 tempC'.unit
143
+ '100 tempC'.unit + '50 tempC'.unit # exception
144
+ '100 tempC'.unit - '50 tempC'.unit # '50 degC'.unit
145
+ '50 tempC'.unit - '100 tempC'.unit # '-50 degC'.unit
146
+ '100 tempC'.unit * [scalar] # '100*scalar tempC'.unit
147
+ '100 tempC'.unit / [scalar] # '100/scalar tempC'.unit
148
+ '100 tempC'.unit * [unit] # exception
149
+ '100 tempC'.unit / [unit] # exception
150
+ '100 tempC'.unit ** N # exception
151
+
152
+ '100 tempC'.unit >> 'degC' #=> '100 degC'.unit
153
+ This conversion references the 0 point on the scale of the temperature unit
154
+
155
+ '100 degC'.unit >> 'tempC' #=> '-173 tempC'.unit
156
+ These conversions are always interpreted as being relative to absolute zero.
157
+ Conversions are probably better done like this...
158
+
159
+ '0 tempC'.unit + '100 degC'.unit #=> '100 tempC'.unit
160
+
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'hoe'
3
+
4
+ Hoe.plugin :yard
5
+
6
+ require './lib/ruby_units/units'
7
+ require './lib/ruby_units/ruby-units'
8
+
9
+ begin
10
+ require 'rcov/rcovtask'
11
+ Rcov::RcovTask.new do |t|
12
+ t.test_files = FileList['test/test*.rb']
13
+ #t.verbose = true # uncomment to see the executed command
14
+ end
15
+ rescue
16
+ end
17
+
18
+ Hoe.spec('ruby-units') do |p|
19
+ p.yard_title = "Ruby-Units"
20
+ p.yard_markup = "markdown"
21
+ p.version = Unit::VERSION
22
+ p.rubyforge_name = 'ruby-units'
23
+ p.summary = %q{A class that performs unit conversions and unit math}
24
+ p.email = 'kevin.olbrich+ruby_units@gmail.com'
25
+ p.url = 'http://github.com/olbrich/ruby-units'
26
+ p.description = "This library handles unit conversions and unit math"
27
+ p.changes = p.paragraphs_of('CHANGELOG.txt', 0..1).join("\n\n")
28
+ p.author = 'Kevin Olbrich, Ph.D'
29
+ end
@@ -0,0 +1,11 @@
1
+ require 'ruby_units/array'
2
+ require 'ruby_units/date'
3
+ require 'ruby_units/time'
4
+ require 'ruby_units/math'
5
+ require 'ruby_units/complex'
6
+ require 'ruby_units/numeric'
7
+ require 'ruby_units/object'
8
+ require 'ruby_units/string'
9
+ require 'ruby_units/units'
10
+ require 'ruby_units/ruby-units'
11
+