Class: Astronoby::Vector

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

Constructor Details

#initializeVector

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

#magnitudeAstronoby::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.

Returns:



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

#xObject

Returns the first component (x-axis).

Returns:

  • (Object)

    the first component (x-axis)



16
17
18
# File 'lib/astronoby/vector.rb', line 16

def x
  self[0]
end

#yObject

Returns the second component (y-axis).

Returns:

  • (Object)

    the second component (y-axis)



21
22
23
# File 'lib/astronoby/vector.rb', line 21

def y
  self[1]
end

#zObject

Returns the third component (z-axis).

Returns:

  • (Object)

    the third component (z-axis)



26
27
28
# File 'lib/astronoby/vector.rb', line 26

def z
  self[2]
end