Class: Weight

Inherits:
Object
  • Object
show all
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

Methods included from Configuration

allowed_units, configure, default_unit, default_unit=

Constructor Details

#initialize(value = 0, unit = default_unit) ⇒ Weight

Class contructor

Parameters:

  • value (Int) (defaults to: 0)

    The weight of the object

  • unit (String) (defaults to: default_unit)

    The unit in which the value is expressed. Currently supports ‘lb’ and ‘kg’

Raises:

  • (TypeError)

    When weight is negative



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

Parameters:

  • other (Weight)

    The other weight to multiply by the current one

Returns:

  • (Weight)

    The product of the two weights

Raises:

  • (TypeError)

    When the argument passed is not a Weight



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

Parameters:

  • other (Weight)

    The other weight to add to the current one

Returns:

  • (Weight)

    The sum of the two weights

Raises:

  • (TypeError)

    When the argument passed is not a Weight



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

Parameters:

  • other (Weight)

    The other weight to substract to the current one

Returns:

  • (Weight)

    The substraction of the two weights

Raises:

  • (TypeError)

    When the argument passed is not a Weight



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

Parameters:

  • other (Weight)

    The weight to divide the current one by

Returns:

  • (Weight)

    The result of the operation

Raises:

  • (TypeError)

    When the argument passed is not a Weight



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

Parameters:

  • other (Weight)

    The other weight to compare to the current one

Returns:

  • (-1, 0, 1)

Raises:

  • (TypeError)

    When the argument passed is not a Weight



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

Parameters:

  • other (Weight)

    The other weight to compare to the current one

Returns:

  • (Bool)

    True if both of the objects have the same weight. False otherwise

Raises:

  • (TypeError)

    When the argument passed is not a Weight



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_fFloat

Returns the weight converted to pounds

Returns:

  • (Float)

    The weight in pounds



38
39
40
# File 'lib/weight.rb', line 38

def to_f
  value.to_f.round(4)
end

#to_iInt

Returns the weight in pounds rounded to the closest integer

Returns:

  • (Int)

    The weight in pounds rounded



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

def to_i
  value.round
end

#to_kgsFloat

Returns the weight converted to pounds

Returns:

  • (Float)

    The weight in pounds



26
27
28
# File 'lib/weight.rb', line 26

def to_kgs
  raw_data_in_kg.round(4)
end

#to_lbsFloat

Returns the weight converted to kilograms

Returns:

  • (Float)

    The weight in kilograms



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

def to_lbs
  (raw_data_in_kg * pounds_per_kilogram).round(4)
end

#unitObject



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

def unit
  @input_unit
end

#valueObject



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

def value
  @input_value
end