Class: Tangle::Edge

Inherits:
Object
  • Object
show all
Includes:
Mixin::Initialize
Defined in:
lib/tangle/edge.rb

Overview

An edge in a graph, connecting two vertices

Direct Known Subclasses

Directed::Edge

Instance Attribute Summary collapse

Attributes included from Mixin::Initialize

#mixins

Instance Method Summary collapse

Constructor Details

#initialize(vertex1, vertex2 = vertex1, name: nil, **kwargs) ⇒ Edge

Create a new edge between vertices

Edge.new(vtx1) => Edge (loop) Edge.new(vtx1, vtx2) => Edge

End users should probably use Graph#add_edge instead.



20
21
22
23
24
# File 'lib/tangle/edge.rb', line 20

def initialize(vertex1, vertex2 = vertex1, name: nil, **kwargs)
  @name = name
  initialize_vertices(vertex1, vertex2)
  initialize_mixins(**kwargs)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/tangle/edge.rb', line 11

def name
  @name
end

Instance Method Details

#[](from_vertex) ⇒ Object



26
27
28
# File 'lib/tangle/edge.rb', line 26

def [](from_vertex)
  @vertices[from_vertex]
end

#each_vertex(&block) ⇒ Object



40
41
42
# File 'lib/tangle/edge.rb', line 40

def each_vertex(&block)
  @vertices.each_key(&block)
end

#include?(vertex) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/tangle/edge.rb', line 44

def include?(vertex)
  each_vertex.include?(vertex)
end

#loop?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/tangle/edge.rb', line 48

def loop?
  @loop
end

#to_sObject Also known as: inspect



34
35
36
37
# File 'lib/tangle/edge.rb', line 34

def to_s
  vertex1, vertex2 = @vertices.keys
  "{#{vertex1}<->#{vertex2}}"
end

#walk(from_vertex) ⇒ Object



30
31
32
# File 'lib/tangle/edge.rb', line 30

def walk(from_vertex)
  @vertices.fetch(from_vertex)
end