Class: MiniGraph::Core::Edge::Directed

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_graph/core/edge.rb

Overview


Directed Edge


Direct Known Subclasses

Undirected

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(origin, destination) ⇒ Directed

Returns a new instance of Directed.



14
15
16
17
# File 'lib/mini_graph/core/edge.rb', line 14

def initialize(origin, destination)
  @origin       = origin
  @destination  = destination
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



12
13
14
# File 'lib/mini_graph/core/edge.rb', line 12

def destination
  @destination
end

#originObject (readonly)

Returns the value of attribute origin.



12
13
14
# File 'lib/mini_graph/core/edge.rb', line 12

def origin
  @origin
end

Instance Method Details

#==(edge) ⇒ Object

cannot use alias or alias_method for this, as subclasses (e.g. UndirectedEdge) do not behave as expected. We’ll be explicit.



25
26
27
# File 'lib/mini_graph/core/edge.rb', line 25

def ==(edge)
  eql?(edge)
end

#eql?(edge) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/mini_graph/core/edge.rb', line 19

def eql?(edge)
  origin == edge.origin && destination == edge.destination
end

#inspectObject

Another case of not using alias to allow subclasses to behave as expected.



34
35
36
# File 'lib/mini_graph/core/edge.rb', line 34

def inspect
  to_s
end

#reverseObject

Reverses the direction of the edge



39
40
41
# File 'lib/mini_graph/core/edge.rb', line 39

def reverse
  self.class.new(destination, origin)
end

#self_loop?Boolean

Indicates a self-looping edge; i.e., an edge that connects a vertex to itself.

Returns:

  • (Boolean)


45
46
47
# File 'lib/mini_graph/core/edge.rb', line 45

def self_loop?
  origin == destination
end

#to_sObject



29
30
31
# File 'lib/mini_graph/core/edge.rb', line 29

def to_s
  "(#{origin} -> #{destination})"
end