Class: Weight
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#initialize(value = 0.0, unit = 'lb') ⇒ Weight
constructor
A new instance of Weight.
- #to_f ⇒ Object
- #to_gms ⇒ Object
- #to_i ⇒ Object
- #to_kgs ⇒ Object
- #to_lbs ⇒ Object
- #to_ozs ⇒ Object
- #translated ⇒ Object
- #unit ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(value = 0.0, unit = 'lb') ⇒ Weight
7 8 9 10 |
# File 'lib/weight_conversion.rb', line 7 def initialize(value=0.0, unit='lb') self.value = value self.unit = unit end |
Instance Method Details
#*(other) ⇒ Object
68 69 70 71 |
# 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) ⇒ Object
48 49 50 51 |
# 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) ⇒ Object
53 54 55 56 |
# 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) ⇒ Object
73 74 75 76 |
# 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) ⇒ Object
58 59 60 61 |
# File 'lib/weight_conversion.rb', line 58 def <=>(other) return if is_not_weight?(other) self.to_gms <=> other.to_gms end |
#==(other) ⇒ Object
63 64 65 66 |
# File 'lib/weight_conversion.rb', line 63 def ==(other) return if is_not_weight?(other) self.to_gms == other.to_gms end |
#to_f ⇒ Object
44 45 46 |
# File 'lib/weight_conversion.rb', line 44 def to_f value.round(4).to_f end |
#to_gms ⇒ Object
24 25 26 |
# File 'lib/weight_conversion.rb', line 24 def to_gms data_in_grams.round(4) end |
#to_i ⇒ Object
40 41 42 |
# File 'lib/weight_conversion.rb', line 40 def to_i value.round end |
#to_kgs ⇒ Object
36 37 38 |
# File 'lib/weight_conversion.rb', line 36 def to_kgs (data_in_grams / grams_per_kilogram).round(4) end |
#to_lbs ⇒ Object
32 33 34 |
# File 'lib/weight_conversion.rb', line 32 def to_lbs (data_in_grams / grams_per_pound).round(4) end |
#to_ozs ⇒ Object
28 29 30 |
# File 'lib/weight_conversion.rb', line 28 def to_ozs (data_in_grams / grams_per_ounce).round(4) end |
#translated ⇒ Object
12 13 14 |
# File 'lib/weight_conversion.rb', line 12 def translated I18n.t("weight.units.#{unit}") end |
#unit ⇒ Object
20 21 22 |
# File 'lib/weight_conversion.rb', line 20 def unit @input_unit end |
#value ⇒ Object
16 17 18 |
# File 'lib/weight_conversion.rb', line 16 def value @input_value end |