Class: Weight

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/weight_conversion.rb

Instance Method Summary collapse

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

Raises:

  • (TypeError)


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

Raises:

  • (TypeError)


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_fObject



44
45
46
# File 'lib/weight_conversion.rb', line 44

def to_f
   value.round(4).to_f
end

#to_gmsObject



24
25
26
# File 'lib/weight_conversion.rb', line 24

def to_gms
   data_in_grams.round(4)
end

#to_iObject



40
41
42
# File 'lib/weight_conversion.rb', line 40

def to_i
   value.round
end

#to_kgsObject



36
37
38
# File 'lib/weight_conversion.rb', line 36

def to_kgs
   (data_in_grams / grams_per_kilogram).round(4)
end

#to_lbsObject



32
33
34
# File 'lib/weight_conversion.rb', line 32

def to_lbs
   (data_in_grams / grams_per_pound).round(4)
end

#to_ozsObject



28
29
30
# File 'lib/weight_conversion.rb', line 28

def to_ozs
   (data_in_grams / grams_per_ounce).round(4)
end

#translatedObject



12
13
14
# File 'lib/weight_conversion.rb', line 12

def translated
 I18n.t("weight.units.#{unit}")
end

#unitObject



20
21
22
# File 'lib/weight_conversion.rb', line 20

def unit
   @input_unit
end

#valueObject



16
17
18
# File 'lib/weight_conversion.rb', line 16

def value
   @input_value
end