Class: Astronoby::Distance

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

Examples:

Create a distance from astronomical units

distance = Astronoby::Distance.from_au(1.0)
distance.km  # => 149597870.7

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meters) ⇒ Distance

Returns a new instance of Distance.

Parameters:

  • meters (Numeric)

    the distance in meters



65
66
67
68
# File 'lib/astronoby/distance.rb', line 65

def initialize(meters)
  @meters = meters
  freeze
end

Instance Attribute Details

#metersNumeric (readonly) Also known as: m

Returns the distance in meters.

Returns:

  • (Numeric)

    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.

Parameters:

  • astronomical_units (Numeric)

    the distance in AU

Returns:



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.

Parameters:

  • kilometers (Numeric)

    the distance in kilometers

Returns:



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.

Parameters:

  • meters (Numeric)

    the distance in meters

Returns:



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.

Parameters:

  • parsecs (Numeric)

    the distance in parsecs

Returns:



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.

Parameters:

  • array (Array<Numeric>)

    array of meter values

Returns:



54
55
56
# File 'lib/astronoby/distance.rb', line 54

def vector_from_meters(array)
  Vector.elements(array.map { from_meters(_1) })
end

.zeroAstronoby::Distance

Returns a zero distance.

Returns:



17
18
19
# File 'lib/astronoby/distance.rb', line 17

def zero
  new(0)
end

Instance Method Details

#+(other) ⇒ Astronoby::Distance

Returns the sum.

Parameters:

Returns:



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.

Parameters:

Returns:



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.

Returns:



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.

Parameters:

Returns:

  • (Integer, nil)

    -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

#abs2Numeric

Returns the square of the distance in meters.

Returns:

  • (Numeric)

    the square of the distance in meters



115
116
117
# File 'lib/astronoby/distance.rb', line 115

def abs2
  meters**2
end

#astronomical_unitsFloat Also known as: au

Returns the distance in astronomical units.

Returns:

  • (Float)

    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

#hashInteger

Returns hash value.

Returns:

  • (Integer)

    hash value



120
121
122
# File 'lib/astronoby/distance.rb', line 120

def hash
  [meters, self.class].hash
end

#kilometersFloat Also known as: km

Returns the distance in kilometers.

Returns:

  • (Float)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    true if the distance is zero



110
111
112
# File 'lib/astronoby/distance.rb', line 110

def zero?
  meters.zero?
end