Class: Trainworks::Route

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

Overview

Route is used to group together the information of an edge of a graph. From and To are two nodes and distance is the weight between them

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to:, distance:) ⇒ Route

from and to must respond to #to_s and distance must respond to #to_f

Parameters:

  • from (Object)

    is the starting point of the route

  • to (Object)

    is the end point of the route

  • distance (Object)

    also known as weight between from and to



13
14
15
16
17
# File 'lib/trainworks/route.rb', line 13

def initialize(from:, to:, distance:)
  self.from     = from.to_s
  self.to       = to.to_s
  self.distance = distance.to_f
end

Instance Attribute Details

#distanceObject

Returns the value of attribute distance.



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

def distance
  @distance
end

#fromObject

Returns the value of attribute from.



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

def from
  @from
end

#toObject

Returns the value of attribute to.



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

def to
  @to
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if other.from, other.to and other.distance have the same value as self

Parameters:

  • other (Object)

Returns:

  • (Boolean)


22
23
24
25
26
# File 'lib/trainworks/route.rb', line 22

def ==(other)
  from == other.from &&
    to == other.to &&
    distance == other.distance
end