Class: Distance
- Inherits:
-
Object
- Object
- Distance
- Defined in:
- lib/distance.rb,
lib/distance/version.rb
Constant Summary collapse
- MARATHON =
new(42195.0)
- VERSION =
'1.0.0'
Class Method Summary collapse
- .feet(n) ⇒ Object
- .inches(n) ⇒ Object
- .kilometers(n) ⇒ Object
- .meters(n) ⇒ Object
- .miles(n) ⇒ Object
Instance Method Summary collapse
- #*(multiplier) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(divisor) ⇒ Object
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
-
#initialize(distance_in_meters) ⇒ Distance
constructor
A new instance of Distance.
- #to_f ⇒ Object
- #to_feet ⇒ Object
- #to_inches ⇒ Object
- #to_kilometers ⇒ Object
- #to_miles ⇒ Object
Constructor Details
#initialize(distance_in_meters) ⇒ Distance
Returns a new instance of Distance.
13 14 15 |
# File 'lib/distance.rb', line 13 def initialize(distance_in_meters) @distance_in_meters = distance_in_meters.to_f end |
Class Method Details
.feet(n) ⇒ Object
33 34 35 |
# File 'lib/distance.rb', line 33 def self.feet(n) new(n * Multiplier::FOOT) end |
.inches(n) ⇒ Object
29 30 31 |
# File 'lib/distance.rb', line 29 def self.inches(n) new(n * Multiplier::INCH) end |
.kilometers(n) ⇒ Object
25 26 27 |
# File 'lib/distance.rb', line 25 def self.kilometers(n) new(n * Multiplier::KILOMETER) end |
.meters(n) ⇒ Object
21 22 23 |
# File 'lib/distance.rb', line 21 def self.meters(n) new(n) end |
.miles(n) ⇒ Object
37 38 39 |
# File 'lib/distance.rb', line 37 def self.miles(n) new(n * Multiplier::MILE) end |
Instance Method Details
#*(multiplier) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/distance.rb', line 75 def *(multiplier) unless multiplier.is_a?(Numeric) raise ArgumentError, 'Can only multiply a Distance with a number' end Distance.meters(to_f * multiplier) end |
#+(other) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/distance.rb', line 61 def +(other) unless other.is_a?(Distance) raise ArgumentError, 'Can only add a Distance to a Distance' end Distance.meters(to_f + other.to_f) end |
#-(other) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/distance.rb', line 68 def -(other) unless other.is_a?(Distance) raise ArgumentError, 'Can only subtract a Distance from a Distance' end Distance.meters(to_f - other.to_f) end |
#/(divisor) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/distance.rb', line 82 def /(divisor) unless divisor.is_a?(Numeric) raise ArgumentError, 'Can only divide a Distance by a number' end Distance.meters(to_f / divisor) end |
#<(other) ⇒ Object
104 105 106 107 |
# File 'lib/distance.rb', line 104 def <(other) return false unless other.is_a?(Distance) to_f < other.to_f end |
#<=(other) ⇒ Object
109 110 111 112 |
# File 'lib/distance.rb', line 109 def <=(other) return false unless other.is_a?(Distance) to_f <= other.to_f end |
#==(other) ⇒ Object
99 100 101 102 |
# File 'lib/distance.rb', line 99 def ==(other) return false unless other.is_a?(Distance) to_f == other.to_f end |
#>(other) ⇒ Object
89 90 91 92 |
# File 'lib/distance.rb', line 89 def >(other) return false unless other.is_a?(Distance) to_f > other.to_f end |
#>=(other) ⇒ Object
94 95 96 97 |
# File 'lib/distance.rb', line 94 def >=(other) return false unless other.is_a?(Distance) to_f >= other.to_f end |
#to_f ⇒ Object
41 42 43 |
# File 'lib/distance.rb', line 41 def to_f distance_in_meters end |
#to_feet ⇒ Object
53 54 55 |
# File 'lib/distance.rb', line 53 def to_feet (distance_in_meters / Multiplier::FOOT) end |
#to_inches ⇒ Object
49 50 51 |
# File 'lib/distance.rb', line 49 def to_inches (distance_in_meters / Multiplier::INCH) end |
#to_kilometers ⇒ Object
45 46 47 |
# File 'lib/distance.rb', line 45 def to_kilometers distance_in_meters / Multiplier::KILOMETER end |
#to_miles ⇒ Object
57 58 59 |
# File 'lib/distance.rb', line 57 def to_miles (distance_in_meters / Multiplier::MILE) end |