README in ruby-units-0.2.0 vs README in ruby-units-0.2.1
- old
+ new
@@ -1,8 +1,8 @@
=Ruby Units
-Version: 0.1.0
+Version: 0.2.1
Kevin C. Olbrich, Ph.D.
http://www.sciwerks.com
@@ -27,10 +27,15 @@
unit = Unit.new("1 kg*m^2/s^2") # complex unit
unit = Unit.new("1 kg m^2 s^-2") # complex unit
unit = Unit("1 mm") # shorthand
unit = "1 mm".to_unit # convert string object
unit = object.to_unit # convert any object using object.to_s
+ unit = U'1 mm'
+ unit = u'1 mm'
+ unit = '1 mm'.unit
+ unit = '1 mm'.u
+
==Rules:
1. only 1 quantity per unit (with 2 exceptions... 6'5" and '8 lbs 8 oz')
2. use SI notation when possible
3. avoid using spaces in unit names
@@ -67,19 +72,49 @@
unit1 = unit >> "ft" # => convert to 'feet'
unit >>= "ft" # => convert and overwrite original object
unit3 = unit1 + unit2 # => resulting object will have the units of unit1
unit3 = unit1 - unit2 # => resulting object will have the units of unit1
- unit1 <=> unit2 # => does comparison on quantities in base units, throws an exception if not compatible
- unit1 === unit2 # => true if units and quantity are the same, even if 'equivalent' by <=>
+ unit1 <=> unit2 # => does comparison on quantities in base units,
+ throws an exception if not compatible
+ unit1 === unit2 # => true if units and quantity are the same, even if
+ 'equivalent' by <=>
+ unit.to('ft') # convert
==Text Output
Units will display themselves nicely based on the preferred abbreviation for the units and prefixes.
Since Unit implements a Unit#to_s, all that is needed in most cases is:
"#{Unit.new('1 mm')}" #=> "1 mm"
The to_s also accepts some options.
- Unit.new('1.5 mm').to_s("%0.2f") # => "1.50 mm". Enter any valid format string
- Unit.new('1.5 mm').to_s("in") # => converts to inches before printing
- Unit.new("2 m").to_s(:ft) #=> returns 6'7"
- Unit.new("100 kg").to_s(:lbs) #=> returns 220 lbs, 7 oz
-
\ No newline at end of file
+ Unit.new('1.5 mm').to_s("%0.2f") # => "1.50 mm". Enter any valid format
+ string. Also accepts strftime format
+ U('1.5 mm').to_s("in") # => converts to inches before printing
+ U("2 m").to_s(:ft) #=> returns 6'7"
+ U("100 kg").to_s(:lbs) #=> returns 220 lbs, 7 oz
+
+
+==Time Helpers
+
+Time, Date, and DateTime objects can have time units added or subtracted.
+
+Time.now + "10 min".u
+
+Several helpers have also been defined.
+
+'min'.since('9/18/06 3:00pm')
+'min'.before('9/18/08 3:00pm')
+'days'.until('1/1/07')
+'5 min'.from(Time.now)
+'5 min'.from_now
+'5 min'.before_now
+'5 min'.before(Time.now)
+'10 min'.ago
+
+==Ranges
+
+[U('0 h')..U('10 h')].each {|x| p x}
+works so long as the starting point has an integer scalar
+
+==Math functions
+All Trig math functions (sin, cos, sinh, ...) can take a unit as their parameter. It will be converted to radians and then used if possible.
+