Class: Astronoby::Distance
- Inherits:
-
Object
- Object
- Astronoby::Distance
- Includes:
- Comparable
- Defined in:
- lib/astronoby/distance.rb
Overview
Represents a distance with meters as its internal representation. Provides conversions between meters, kilometers, astronomical units, and parsecs.
Instance Attribute Summary collapse
-
#meters ⇒ Numeric
(also: #m)
readonly
The distance in meters.
Class Method Summary collapse
-
.from_astronomical_units(astronomical_units) ⇒ Astronoby::Distance
(also: from_au)
A new Distance.
-
.from_kilometers(kilometers) ⇒ Astronoby::Distance
(also: from_km)
A new Distance.
-
.from_meters(meters) ⇒ Astronoby::Distance
(also: from_m)
A new Distance.
-
.from_parsecs(parsecs) ⇒ Astronoby::Distance
(also: from_pc)
A new Distance.
-
.vector_from_meters(array) ⇒ Astronoby::Vector<Astronoby::Distance>
(also: vector_from_m)
A vector of Distances.
-
.zero ⇒ Astronoby::Distance
A zero distance.
Instance Method Summary collapse
-
#+(other) ⇒ Astronoby::Distance
The sum.
-
#-(other) ⇒ Astronoby::Distance
The difference.
-
#-@ ⇒ Astronoby::Distance
The negated distance.
-
#<=>(other) ⇒ Integer?
-1, 0, or 1; nil if not comparable.
-
#abs2 ⇒ Numeric
The square of the distance in meters.
-
#astronomical_units ⇒ Float
(also: #au)
The distance in astronomical units.
-
#hash ⇒ Integer
Hash value.
-
#initialize(meters) ⇒ Distance
constructor
A new instance of Distance.
-
#kilometers ⇒ Float
(also: #km)
The distance in kilometers.
-
#negative? ⇒ Boolean
True if the distance is negative.
-
#positive? ⇒ Boolean
True if the distance is positive.
-
#zero? ⇒ Boolean
True if the distance is zero.
Constructor Details
#initialize(meters) ⇒ Distance
Returns a new instance of Distance.
65 66 67 68 |
# File 'lib/astronoby/distance.rb', line 65 def initialize(meters) @meters = meters freeze end |
Instance Attribute Details
#meters ⇒ Numeric (readonly) Also known as: m
Returns the distance in meters.
61 62 63 |
# File 'lib/astronoby/distance.rb', line 61 def meters @meters end |
Class Method Details
.from_astronomical_units(astronomical_units) ⇒ Astronoby::Distance Also known as: from_au
Returns a new Distance.
38 39 40 41 |
# File 'lib/astronoby/distance.rb', line 38 def from_astronomical_units(astronomical_units) meters = astronomical_units * Constants::ASTRONOMICAL_UNIT_IN_METERS from_meters(meters) end |
.from_kilometers(kilometers) ⇒ Astronoby::Distance Also known as: from_km
Returns a new Distance.
30 31 32 33 |
# File 'lib/astronoby/distance.rb', line 30 def from_kilometers(kilometers) meters = kilometers * Constants::KILOMETER_IN_METERS from_meters(meters) end |
.from_meters(meters) ⇒ Astronoby::Distance Also known as: from_m
Returns a new Distance.
23 24 25 |
# File 'lib/astronoby/distance.rb', line 23 def from_meters(meters) new(meters) end |
.from_parsecs(parsecs) ⇒ Astronoby::Distance Also known as: from_pc
Returns a new Distance.
46 47 48 49 |
# File 'lib/astronoby/distance.rb', line 46 def from_parsecs(parsecs) meters = parsecs * Constants::PARSEC_IN_METERS from_meters(meters) end |
.vector_from_meters(array) ⇒ Astronoby::Vector<Astronoby::Distance> Also known as: vector_from_m
Returns a vector of Distances.
54 55 56 |
# File 'lib/astronoby/distance.rb', line 54 def vector_from_meters(array) Vector.elements(array.map { from_meters(_1) }) end |
.zero ⇒ Astronoby::Distance
Returns a zero distance.
17 18 19 |
# File 'lib/astronoby/distance.rb', line 17 def zero new(0) end |
Instance Method Details
#+(other) ⇒ Astronoby::Distance
Returns the sum.
84 85 86 |
# File 'lib/astronoby/distance.rb', line 84 def +(other) self.class.from_meters(meters + other.meters) end |
#-(other) ⇒ Astronoby::Distance
Returns the difference.
90 91 92 |
# File 'lib/astronoby/distance.rb', line 90 def -(other) self.class.from_meters(@meters - other.meters) end |
#-@ ⇒ Astronoby::Distance
Returns the negated distance.
95 96 97 |
# File 'lib/astronoby/distance.rb', line 95 def -@ self.class.from_meters(-@meters) end |
#<=>(other) ⇒ Integer?
Returns -1, 0, or 1; nil if not comparable.
126 127 128 129 130 |
# File 'lib/astronoby/distance.rb', line 126 def <=>(other) return unless other.is_a?(self.class) meters <=> other.meters end |
#abs2 ⇒ Numeric
Returns the square of the distance in meters.
115 116 117 |
# File 'lib/astronoby/distance.rb', line 115 def abs2 meters**2 end |
#astronomical_units ⇒ Float Also known as: au
Returns the distance in astronomical units.
77 78 79 |
# File 'lib/astronoby/distance.rb', line 77 def astronomical_units @meters / Constants::ASTRONOMICAL_UNIT_IN_METERS.to_f end |
#hash ⇒ Integer
Returns hash value.
120 121 122 |
# File 'lib/astronoby/distance.rb', line 120 def hash [meters, self.class].hash end |
#kilometers ⇒ Float Also known as: km
Returns the distance in kilometers.
71 72 73 |
# File 'lib/astronoby/distance.rb', line 71 def kilometers @meters / Constants::KILOMETER_IN_METERS.to_f end |
#negative? ⇒ Boolean
Returns true if the distance is negative.
105 106 107 |
# File 'lib/astronoby/distance.rb', line 105 def negative? meters < 0 end |
#positive? ⇒ Boolean
Returns true if the distance is positive.
100 101 102 |
# File 'lib/astronoby/distance.rb', line 100 def positive? meters > 0 end |
#zero? ⇒ Boolean
Returns true if the distance is zero.
110 111 112 |
# File 'lib/astronoby/distance.rb', line 110 def zero? meters.zero? end |