ruby-units 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -140,4 +140,8 @@ Change Log for Ruby-units
140
140
  * Time math will now return a DateTime if it goes out of
141
141
  range.
142
142
 
143
- 2006-11-20 0.3.5 * Minor bug fixes
143
+ 2006-11-20 0.3.5 * Minor bug fixes
144
+ * to_int now coerces the result to an actual Integer,
145
+ but only works properly for unitless Units.
146
+
147
+ 2006-12-05 0.3.6 * Fixed bug where (unit/unit).ceil would fail
data/README.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  =Ruby Units
2
2
 
3
- Version: 0.3.5
3
+ Version: 0.3.6
4
4
 
5
5
  Kevin C. Olbrich, Ph.D.
6
6
  kevin.olbrich@gmail.com
@@ -80,6 +80,8 @@ Units can be converted to other units in a couple of ways.
80
80
  unit1 === unit2 # => true if units and quantity are the same, even if
81
81
  'equivalent' by <=>
82
82
  unit.to('ft') # convert
83
+ unit1 + unit2 >> "ft" # converts result of math to 'ft'
84
+ (unit1 + unit2).to('ft') # converts result to 'ft'
83
85
 
84
86
  ==Text Output
85
87
  Units will display themselves nicely based on the preferred abbreviation for the units and prefixes.
@@ -98,9 +100,11 @@ The to_s also accepts some options.
98
100
 
99
101
  Time, Date, and DateTime objects can have time units added or subtracted.
100
102
 
101
- Time.now + "10 min".u
103
+ Time.now + "10 min".unit
102
104
 
103
105
  Several helpers have also been defined.
106
+ Note: If you include the 'Chronic' gem, you can specify times in natural
107
+ language.
104
108
 
105
109
  'min'.since('9/18/06 3:00pm')
106
110
  'min'.before('9/18/08 3:00pm')
data/lib/ruby-units.rb CHANGED
@@ -2,7 +2,7 @@ require 'mathn'
2
2
  require 'rational'
3
3
  require 'date'
4
4
  require 'parsedate'
5
- # = Ruby Units 0.3.5
5
+ # = Ruby Units 0.3.6
6
6
  #
7
7
  # Copyright 2006 by Kevin C. Olbrich, Ph.D.
8
8
  #
@@ -40,7 +40,7 @@ require 'parsedate'
40
40
  class Unit < Numeric
41
41
  require 'units'
42
42
  # pre-generate hashes from unit definitions for performance.
43
- VERSION = '0.3.5'
43
+ VERSION = '0.3.6'
44
44
  @@USER_DEFINITIONS = {}
45
45
  @@PREFIX_VALUES = {}
46
46
  @@PREFIX_MAP = {}
@@ -688,6 +688,7 @@ class Unit < Numeric
688
688
 
689
689
  # negates the scalar of the Unit
690
690
  def -@
691
+ return -@scalar if self.unitless?
691
692
  Unit.new(-@scalar,@numerator,@denominator)
692
693
  end
693
694
 
@@ -697,10 +698,12 @@ class Unit < Numeric
697
698
  end
698
699
 
699
700
  def ceil
701
+ return @scalar.ceil if self.unitless?
700
702
  Unit.new(@scalar.ceil, @numerator, @denominator)
701
703
  end
702
704
 
703
705
  def floor
706
+ return @scalar.floor if self.unitless?
704
707
  Unit.new(@scalar.floor, @numerator, @denominator)
705
708
  end
706
709
 
@@ -718,6 +721,7 @@ class Unit < Numeric
718
721
  alias :to_i :to_int
719
722
 
720
723
  def truncate
724
+ return @scalar.truncate if self.unitless?
721
725
  Unit.new(@scalar.truncate, @numerator, @denominator)
722
726
  end
723
727
 
@@ -726,6 +730,7 @@ class Unit < Numeric
726
730
  end
727
731
 
728
732
  def round
733
+ return @scalar.round if self.unitless?
729
734
  Unit.new(@scalar.round, @numerator, @denominator)
730
735
  end
731
736
 
@@ -509,12 +509,14 @@ class TestRubyUnits < Test::Unit::TestCase
509
509
  unit1 = Unit.new("1.1 mm")
510
510
  unit2 = Unit.new("2 mm")
511
511
  assert_equal unit2, unit1.ceil
512
+ assert_equal ('1 mm'.unit / '1 mm'.unit).ceil, 1
512
513
  end
513
514
 
514
515
  def test_floor
515
516
  unit1 = Unit.new("1.1 mm")
516
517
  unit2 = Unit.new("1 mm")
517
518
  assert_equal unit2, unit1.floor
519
+ assert_equal ('1 mm'.unit / '1 mm'.unit).floor, 1
518
520
  end
519
521
 
520
522
  def test_to_int
@@ -526,12 +528,14 @@ class TestRubyUnits < Test::Unit::TestCase
526
528
  unit1 = Unit.new("1.1 mm")
527
529
  unit2 = Unit.new("1 mm")
528
530
  assert_equal unit2, unit1.truncate
531
+ assert_equal (unit1/unit2).truncate, 1
529
532
  end
530
533
 
531
534
  def test_round
532
535
  unit1 = Unit.new("1.1 mm")
533
536
  unit2 = Unit.new("1 mm")
534
537
  assert_equal unit2, unit1.round
538
+ assert_equal (unit1/unit2).round, 1
535
539
  end
536
540
 
537
541
  def test_zero?
@@ -803,7 +807,7 @@ class TestRubyUnits < Test::Unit::TestCase
803
807
  end
804
808
 
805
809
  def test_zero
806
- assert_nothing_raised { Unit.new(0) }
810
+ assert_nothing_raised { Unit.new(0) }
807
811
  end
808
812
 
809
813
  def test_divide_results_in_unitless
@@ -811,4 +815,5 @@ class TestRubyUnits < Test::Unit::TestCase
811
815
  b = '10 mg/ml'.unit
812
816
  assert_equal a/b, 1
813
817
  end
818
+
814
819
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: ruby-units
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.5
7
- date: 2006-11-30 00:00:00 -05:00
6
+ version: 0.3.6
7
+ date: 2006-12-05 00:00:00 -05:00
8
8
  summary: A model that performs unit conversions and unit math
9
9
  require_paths:
10
10
  - lib