Class: Astronoby::Velocity
- Inherits:
-
Object
- Object
- Astronoby::Velocity
- Includes:
- Comparable
- Defined in:
- lib/astronoby/velocity.rb
Overview
Represents a velocity with meters per second as its internal representation. Provides conversions between m/s, km/s, km/day, and AU/day.
Instance Attribute Summary collapse
-
#meters_per_second ⇒ Numeric
(also: #mps)
readonly
The velocity in meters per second.
Class Method Summary collapse
-
.from_astronomical_units_per_day(astronomical_units_per_day) ⇒ Astronoby::Velocity
(also: from_aupd)
A new Velocity.
-
.from_kilometers_per_day(kilometers_per_day) ⇒ Astronoby::Velocity
(also: from_kmpd)
A new Velocity.
-
.from_kilometers_per_second(kilometers_per_second) ⇒ Astronoby::Velocity
(also: from_kmps)
A new Velocity.
-
.from_meters_per_second(meters_per_second) ⇒ Astronoby::Velocity
(also: from_mps)
A new Velocity.
-
.light_speed ⇒ Astronoby::Velocity
The speed of light in vacuum.
-
.vector_from_astronomical_units_per_day(array) ⇒ Astronoby::Vector<Astronoby::Velocity>
(also: vector_from_aupd)
A vector of Velocities.
-
.vector_from_meters_per_second(array) ⇒ Astronoby::Vector<Astronoby::Velocity>
(also: vector_from_mps)
A vector of Velocities.
-
.zero ⇒ Astronoby::Velocity
A zero velocity.
Instance Method Summary collapse
-
#+(other) ⇒ Astronoby::Velocity
The sum.
-
#-(other) ⇒ Astronoby::Velocity
The difference.
-
#-@ ⇒ Astronoby::Velocity
The negated velocity.
-
#<=>(other) ⇒ Integer?
-1, 0, or 1; nil if not comparable.
-
#abs2 ⇒ Numeric
The square of the velocity in (m/s)^2.
-
#astronomical_units_per_day ⇒ Float
(also: #aupd)
The velocity in astronomical units per day.
-
#hash ⇒ Integer
Hash value.
-
#initialize(meters_per_second) ⇒ Velocity
constructor
A new instance of Velocity.
-
#kilometers_per_day ⇒ Float
(also: #kmpd)
The velocity in kilometers per day.
-
#kilometers_per_second ⇒ Float
(also: #kmps)
The velocity in kilometers per second.
-
#negative? ⇒ Boolean
True if the velocity is negative.
-
#positive? ⇒ Boolean
True if the velocity is positive.
-
#zero? ⇒ Boolean
True if the velocity is zero.
Constructor Details
#initialize(meters_per_second) ⇒ Velocity
Returns a new instance of Velocity.
80 81 82 83 |
# File 'lib/astronoby/velocity.rb', line 80 def initialize(meters_per_second) @meters_per_second = meters_per_second freeze end |
Instance Attribute Details
#meters_per_second ⇒ Numeric (readonly) Also known as: mps
Returns the velocity in meters per second.
76 77 78 |
# File 'lib/astronoby/velocity.rb', line 76 def meters_per_second @meters_per_second end |
Class Method Details
.from_astronomical_units_per_day(astronomical_units_per_day) ⇒ Astronoby::Velocity Also known as: from_aupd
Returns a new Velocity.
48 49 50 51 52 |
# File 'lib/astronoby/velocity.rb', line 48 def from_astronomical_units_per_day(astronomical_units_per_day) meters_per_second = astronomical_units_per_day * Constants::ASTRONOMICAL_UNIT_IN_METERS / Constants::SECONDS_PER_DAY from_meters_per_second(meters_per_second) end |
.from_kilometers_per_day(kilometers_per_day) ⇒ Astronoby::Velocity Also known as: from_kmpd
Returns a new Velocity.
39 40 41 42 43 |
# File 'lib/astronoby/velocity.rb', line 39 def from_kilometers_per_day(kilometers_per_day) meters_per_second = kilometers_per_day * Constants::KILOMETER_IN_METERS / Constants::SECONDS_PER_DAY from_meters_per_second(meters_per_second) end |
.from_kilometers_per_second(kilometers_per_second) ⇒ Astronoby::Velocity Also known as: from_kmps
Returns a new Velocity.
30 31 32 33 34 |
# File 'lib/astronoby/velocity.rb', line 30 def from_kilometers_per_second(kilometers_per_second) meters_per_second = kilometers_per_second * Constants::KILOMETER_IN_METERS from_meters_per_second(meters_per_second) end |
.from_meters_per_second(meters_per_second) ⇒ Astronoby::Velocity Also known as: from_mps
Returns a new Velocity.
23 24 25 |
# File 'lib/astronoby/velocity.rb', line 23 def from_meters_per_second(meters_per_second) new(meters_per_second) end |
.light_speed ⇒ Astronoby::Velocity
Returns the speed of light in vacuum.
70 71 72 |
# File 'lib/astronoby/velocity.rb', line 70 def light_speed from_meters_per_second(Constants::LIGHT_SPEED_M_PER_S) end |
.vector_from_astronomical_units_per_day(array) ⇒ Astronoby::Vector<Astronoby::Velocity> Also known as: vector_from_aupd
Returns a vector of Velocities.
64 65 66 |
# File 'lib/astronoby/velocity.rb', line 64 def vector_from_astronomical_units_per_day(array) Vector.elements(array.map { from_aupd(_1) }) end |
.vector_from_meters_per_second(array) ⇒ Astronoby::Vector<Astronoby::Velocity> Also known as: vector_from_mps
Returns a vector of Velocities.
57 58 59 |
# File 'lib/astronoby/velocity.rb', line 57 def vector_from_meters_per_second(array) Vector.elements(array.map { from_mps(_1) }) end |
.zero ⇒ Astronoby::Velocity
Returns a zero velocity.
17 18 19 |
# File 'lib/astronoby/velocity.rb', line 17 def zero new(0) end |
Instance Method Details
#+(other) ⇒ Astronoby::Velocity
Returns the sum.
107 108 109 110 111 |
# File 'lib/astronoby/velocity.rb', line 107 def +(other) self.class.from_meters_per_second( @meters_per_second + other.meters_per_second ) end |
#-(other) ⇒ Astronoby::Velocity
Returns the difference.
115 116 117 118 119 |
# File 'lib/astronoby/velocity.rb', line 115 def -(other) self.class.from_meters_per_second( @meters_per_second - other.meters_per_second ) end |
#-@ ⇒ Astronoby::Velocity
Returns the negated velocity.
122 123 124 |
# File 'lib/astronoby/velocity.rb', line 122 def -@ self.class.from_meters_per_second(-@meters_per_second) end |
#<=>(other) ⇒ Integer?
Returns -1, 0, or 1; nil if not comparable.
153 154 155 156 157 |
# File 'lib/astronoby/velocity.rb', line 153 def <=>(other) return unless other.is_a?(self.class) meters_per_second <=> other.meters_per_second end |
#abs2 ⇒ Numeric
Returns the square of the velocity in (m/s)^2.
142 143 144 |
# File 'lib/astronoby/velocity.rb', line 142 def abs2 @meters_per_second**2 end |
#astronomical_units_per_day ⇒ Float Also known as: aupd
Returns the velocity in astronomical units per day.
99 100 101 102 |
# File 'lib/astronoby/velocity.rb', line 99 def astronomical_units_per_day @meters_per_second * Constants::SECONDS_PER_DAY / Constants::ASTRONOMICAL_UNIT_IN_METERS end |
#hash ⇒ Integer
Returns hash value.
147 148 149 |
# File 'lib/astronoby/velocity.rb', line 147 def hash [@meters_per_second, self.class].hash end |
#kilometers_per_day ⇒ Float Also known as: kmpd
Returns the velocity in kilometers per day.
92 93 94 95 |
# File 'lib/astronoby/velocity.rb', line 92 def kilometers_per_day @meters_per_second * Constants::SECONDS_PER_DAY / Constants::KILOMETER_IN_METERS end |
#kilometers_per_second ⇒ Float Also known as: kmps
Returns the velocity in kilometers per second.
86 87 88 |
# File 'lib/astronoby/velocity.rb', line 86 def kilometers_per_second @meters_per_second / Constants::KILOMETER_IN_METERS.to_f end |
#negative? ⇒ Boolean
Returns true if the velocity is negative.
132 133 134 |
# File 'lib/astronoby/velocity.rb', line 132 def negative? @meters_per_second < 0 end |
#positive? ⇒ Boolean
Returns true if the velocity is positive.
127 128 129 |
# File 'lib/astronoby/velocity.rb', line 127 def positive? @meters_per_second > 0 end |
#zero? ⇒ Boolean
Returns true if the velocity is zero.
137 138 139 |
# File 'lib/astronoby/velocity.rb', line 137 def zero? @meters_per_second.zero? end |