Class: Weight
- Inherits:
-
Object
- Object
- Weight
- Includes:
- Comparable, Configuration
- Defined in:
- lib/weight.rb,
lib/weight/version.rb
Defined Under Namespace
Modules: Configuration
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Method Summary collapse
-
#*(other) ⇒ Weight
Multiplication operation.
-
#+(other) ⇒ Weight
Addition operation.
-
#-(other) ⇒ Weight
Substract operation.
-
#/(other) ⇒ Weight
Division operation.
-
#<=>(other) ⇒ -1, ...
Comparison operator.
-
#==(other) ⇒ Bool
Comparison operator.
-
#initialize(value = 0, unit = default_unit) ⇒ Weight
constructor
Class contructor.
-
#to_f ⇒ Float
Returns the weight converted to pounds.
-
#to_i ⇒ Int
Returns the weight in pounds rounded to the closest integer.
-
#to_kgs ⇒ Float
Returns the weight converted to pounds.
-
#to_lbs ⇒ Float
Returns the weight converted to kilograms.
- #unit ⇒ Object
- #value ⇒ Object
Methods included from Configuration
allowed_units, configure, default_unit, default_unit=
Constructor Details
#initialize(value = 0, unit = default_unit) ⇒ Weight
Class contructor
12 13 14 |
# File 'lib/weight.rb', line 12 def initialize(value = 0, unit = default_unit) self.value, self.unit = value, unit end |
Instance Method Details
#*(other) ⇒ Weight
Multiplication operation
89 90 91 92 |
# File 'lib/weight.rb', line 89 def *(other) raise TypeError, 'You can only multiply a weight by a number' unless other.is_a?(Numeric) self.class.new(value * other, unit) end |
#+(other) ⇒ Weight
Addition operation
52 53 54 55 |
# File 'lib/weight.rb', line 52 def +(other) raise TypeError, 'You can only add weights' unless other.is_a?(Weight) self.class.new(raw_data_in_kg + other.to_kgs, :kg) end |
#-(other) ⇒ Weight
Substract operation
80 81 82 83 |
# File 'lib/weight.rb', line 80 def -(other) raise TypeError, 'You can only substract weights' unless other.is_a?(Weight) self.class.new(raw_data_in_kg - other.to_kgs, :kg) end |
#/(other) ⇒ Weight
Division operation
98 99 100 101 |
# File 'lib/weight.rb', line 98 def /(other) raise TypeError, 'You can only divide a weight by a number' unless other.is_a?(Numeric) self.class.new(value / other, unit) end |
#<=>(other) ⇒ -1, ...
Comparison operator
71 72 73 74 |
# File 'lib/weight.rb', line 71 def <=>(other) raise TypeError, 'You can only compare weights' unless other.is_a?(Weight) self.to_kgs <=> other.to_kgs end |
#==(other) ⇒ Bool
Comparison operator
62 63 64 65 |
# File 'lib/weight.rb', line 62 def ==(other) raise TypeError, 'You can only compare weights' unless other.is_a?(Weight) raw_data_in_kg.round(4) == other.to_kgs end |
#to_f ⇒ Float
Returns the weight converted to pounds
38 39 40 |
# File 'lib/weight.rb', line 38 def to_f value.to_f.round(4) end |
#to_i ⇒ Int
Returns the weight in pounds rounded to the closest integer
44 45 46 |
# File 'lib/weight.rb', line 44 def to_i value.round end |
#to_kgs ⇒ Float
Returns the weight converted to pounds
26 27 28 |
# File 'lib/weight.rb', line 26 def to_kgs raw_data_in_kg.round(4) end |
#to_lbs ⇒ Float
Returns the weight converted to kilograms
32 33 34 |
# File 'lib/weight.rb', line 32 def to_lbs (raw_data_in_kg * pounds_per_kilogram).round(4) end |
#unit ⇒ Object
20 21 22 |
# File 'lib/weight.rb', line 20 def unit @input_unit end |
#value ⇒ Object
16 17 18 |
# File 'lib/weight.rb', line 16 def value @input_value end |