Class: Astronoby::Vector
- Inherits:
-
Vector
- Object
- Vector
- Astronoby::Vector
- Defined in:
- lib/astronoby/vector.rb
Overview
A 3D vector extending Ruby’s Vector class. Provides named component accessors and type-aware magnitude computation for Distance and Velocity vectors.
Instance Method Summary collapse
-
#initialize ⇒ Vector
constructor
A new instance of Vector.
-
#magnitude ⇒ Astronoby::Distance, ...
(also: #norm, #r)
Returns the Euclidean magnitude of the vector.
-
#x ⇒ Object
The first component (x-axis).
-
#y ⇒ Object
The second component (y-axis).
-
#z ⇒ Object
The third component (z-axis).
Constructor Details
#initialize ⇒ Vector
Returns a new instance of Vector.
10 11 12 13 |
# File 'lib/astronoby/vector.rb', line 10 def initialize(...) super freeze end |
Instance Method Details
#magnitude ⇒ Astronoby::Distance, ... Also known as: norm, r
Returns the Euclidean magnitude of the vector. If all elements are Distance or Velocity instances, the result is wrapped in the same type.
34 35 36 37 38 39 40 41 42 |
# File 'lib/astronoby/vector.rb', line 34 def magnitude if all? { _1.is_a?(Astronoby::Distance) } Astronoby::Distance.new(super) elsif all? { _1.is_a?(Astronoby::Velocity) } Astronoby::Velocity.new(super) else super end end |
#x ⇒ Object
Returns the first component (x-axis).
16 17 18 |
# File 'lib/astronoby/vector.rb', line 16 def x self[0] end |
#y ⇒ Object
Returns the second component (y-axis).
21 22 23 |
# File 'lib/astronoby/vector.rb', line 21 def y self[1] end |
#z ⇒ Object
Returns the third component (z-axis).
26 27 28 |
# File 'lib/astronoby/vector.rb', line 26 def z self[2] end |