lib/convert_unit/base.rb in convert_unit-0.2.0 vs lib/convert_unit/base.rb in convert_unit-1.0.0

- old
+ new

@@ -1,12 +1,13 @@ class Base + require 'bigdecimal' attr_accessor :value, :unit - def initialize(value, unit, valid_units) + def initialize(value, unit, valid_units, precision = 16) raise TypeError, 'no implicit conversion of String into Integer' unless value.is_a? Numeric raise TypeError, 'Invalid Unit Type' unless valid_units.include?(unit.to_s.downcase) - @value = value + @value = BigDecimal.new value, precision @unit = unit.downcase end def ==(other) a_to_b = one_unit_a_to_b(unit, other.unit) * value @@ -27,10 +28,30 @@ b_to_a = other.to(unit) self.class.new(value - b_to_a.value, unit) end def inspect - "#{@value}#{@unit}" + "#{value} #{unit}" + end + + def to_c + "#{value.to_c} #{unit}" + end + + def to_r; + "#{value.to_r} #{unit}" + end + + def to_f + "#{value.to_f} #{unit}" + end + + def to_i + "#{value.to_i} #{unit}" + end + + def to_s + "#{value.to_s} #{unit}" end protected def one_unit_a_to_b(unit_a, unit_b)