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



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.



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



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

def x
  self[0]
end

#yObject



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

def y
  self[1]
end

#zObject



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

def z
  self[2]
end