class Weight
Public Class Methods
new(value=0.0, unit='lb')
click to toggle source
# File lib/weight_conversion.rb, line 7 def initialize(value=0.0, unit='lb') self.value = value self.unit = unit end
Public Instance Methods
*(other)
click to toggle source
# File lib/weight_conversion.rb, line 68 def *(other) raise TypeError, 'You can only multiply by a number' unless other.is_a?(Numeric) self.class.new(value * other, unit) end
+(other)
click to toggle source
# File lib/weight_conversion.rb, line 48 def +(other) return if is_not_weight?(other) self.class.new(value + other_value(other), unit) end
-(other)
click to toggle source
# File lib/weight_conversion.rb, line 53 def -(other) return if is_not_weight?(other) self.class.new(value - other_value(other), unit) end
/(other)
click to toggle source
# File lib/weight_conversion.rb, line 73 def /(other) raise TypeError, 'You can only divide by a number' unless other.is_a?(Numeric) self.class.new(value / other, unit) end
<=>(other)
click to toggle source
# File lib/weight_conversion.rb, line 58 def <=>(other) return if is_not_weight?(other) self.to_gms <=> other.to_gms end
==(other)
click to toggle source
# File lib/weight_conversion.rb, line 63 def ==(other) return if is_not_weight?(other) self.to_gms == other.to_gms end
to_f()
click to toggle source
# File lib/weight_conversion.rb, line 44 def to_f value.round(4).to_f end
to_gms()
click to toggle source
# File lib/weight_conversion.rb, line 24 def to_gms data_in_grams.round(4) end
to_i()
click to toggle source
# File lib/weight_conversion.rb, line 40 def to_i value.round end
to_kgs()
click to toggle source
# File lib/weight_conversion.rb, line 36 def to_kgs (data_in_grams / grams_per_kilogram).round(4) end
to_lbs()
click to toggle source
# File lib/weight_conversion.rb, line 32 def to_lbs (data_in_grams / grams_per_pound).round(4) end
to_ozs()
click to toggle source
# File lib/weight_conversion.rb, line 28 def to_ozs (data_in_grams / grams_per_ounce).round(4) end
translated()
click to toggle source
# File lib/weight_conversion.rb, line 12 def translated I18n.t("weight.units.#{unit}") end
unit()
click to toggle source
# File lib/weight_conversion.rb, line 20 def unit @input_unit end
value()
click to toggle source
# File lib/weight_conversion.rb, line 16 def value @input_value end
Private Instance Methods
allowed_units()
click to toggle source
# File lib/weight_conversion.rb, line 92 def allowed_units [:gm, :oz, :lb, :kg] end
data_in_grams()
click to toggle source
# File lib/weight_conversion.rb, line 109 def data_in_grams case unit when :gm value when :oz value * grams_per_ounce when :lb value * grams_per_pound when :kg value * grams_per_kilogram else raise TypeError, 'Unit is not valid.' end end
grams_per_kilogram()
click to toggle source
# File lib/weight_conversion.rb, line 132 def grams_per_kilogram 1000 end
grams_per_ounce()
click to toggle source
# File lib/weight_conversion.rb, line 124 def grams_per_ounce 28.34952 end
grams_per_pound()
click to toggle source
# File lib/weight_conversion.rb, line 128 def grams_per_pound 453.5923 end
is_not_weight?(other)
click to toggle source
# File lib/weight_conversion.rb, line 136 def is_not_weight?(other) return false if other.is_a?(Weight) raise TypeError, 'Not a Weight class.' return true end
other_value(other)
click to toggle source
# File lib/weight_conversion.rb, line 96 def other_value(other) case unit when :gm other.to_gms when :oz other.to_ozs when :lb other.to_lbs when :kg other.to_kgs end end
unit=(unit)
click to toggle source
# File lib/weight_conversion.rb, line 86 def unit=(unit) unit = unit.to_s.downcase.to_sym raise ArgumentError, "Allowed unit types #{allowed_units.inspect}" unless allowed_units.include?(unit) @input_unit = unit end
value=(value)
click to toggle source
# File lib/weight_conversion.rb, line 80 def value=(value) raise TypeError, 'Value cannot be negative' if value < 0 raise TypeError, 'Value must be Numeric' unless value.is_a? Numeric @input_value = value end