Class: TrackAndDistance

Inherits:
Object
  • Object
show all
Defined in:
lib/track_and_distance.rb

Overview

Holds a bearing and distance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bearing, distance, radians = false) ⇒ TrackAndDistance

Returns a new instance of TrackAndDistance.

Parameters:

  • Bearing (String, Numeric, #to_radian, #to_f)

    can be a String or Numeric or any object with to_radians and to_f

  • distance (Numeric)
  • radians (true, false, :radians) (defaults to: false)

    Bearing is in degrees unless radians == true (or set to :radians).



13
14
15
16
# File 'lib/track_and_distance.rb', line 13

def initialize(bearing, distance, radians = false)
  @bearing = Angle.new(bearing, radians)
  @distance = distance
end

Instance Attribute Details

#bearingAngle

Returns:



6
7
8
# File 'lib/track_and_distance.rb', line 6

def bearing
  @bearing
end

#distanceFloat

Returns:

  • (Float)


8
9
10
# File 'lib/track_and_distance.rb', line 8

def distance
  @distance
end

Instance Method Details

#to_aryArray Also known as: deconstruct

Returns with members bearing and distance.

Returns:

  • (Array)

    with members bearing and distance.



32
33
34
# File 'lib/track_and_distance.rb', line 32

def to_ary
  return [ @bearing, @distance ]
end

#to_hashHash Also known as: deconstruct_keys

Returns with keys :bearing and :distance.

Returns:

  • (Hash)

    with keys :bearing and :distance



37
38
39
# File 'lib/track_and_distance.rb', line 37

def to_hash
  return { bearing: @bearing, distance: @distance }
end

#to_s(fmt = nil) ⇒ String

format string fmt is currently just for the bearing angle. Need to change this to include the distance is single format string.

Parameters:

  • fmt (String) (defaults to: nil)

    Optional format string passed to Coordinate#strf

Returns:

  • (String)

    Bearing angle and distance in meters.



22
23
24
25
26
27
28
29
# File 'lib/track_and_distance.rb', line 22

def to_s(fmt = nil)
  if fmt
    # needs work to include distance as well as angle fmt.
    return "#{@bearing.strf(fmt)} #{distance.round(4)}m"
  else
    return "#{@bearing.strf} #{distance.round(4)}m"
  end
end