Class: Graphsrb::Edge

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

Direct Known Subclasses

DiEdge

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id1, id2, args = {}) ⇒ Edge

Returns a new instance of Edge.



4
5
6
7
8
9
10
11
12
# File 'lib/graphsrb/edge.rb', line 4

def initialize(id1, id2, args={})
  if id1 == id2
    raise Graphsrb::EdgeInitializationError, "Vertex id's must be different from each other"
  end

  @vertex1 = Graphsrb::Vertex.new(id1)
  @vertex2 = Graphsrb::Vertex.new(id2)
  @weight = args.fetch(:weight, 1)
end

Instance Attribute Details

#vertex1Object (readonly) Also known as: initial_vertex

Returns the value of attribute vertex1.



3
4
5
# File 'lib/graphsrb/edge.rb', line 3

def vertex1
  @vertex1
end

#vertex2Object (readonly) Also known as: terminal_vertex

Returns the value of attribute vertex2.



3
4
5
# File 'lib/graphsrb/edge.rb', line 3

def vertex2
  @vertex2
end

#weightObject (readonly)

Returns the value of attribute weight.



3
4
5
# File 'lib/graphsrb/edge.rb', line 3

def weight
  @weight
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
20
# File 'lib/graphsrb/edge.rb', line 17

def ==(other)
  (vertex1.id == other.vertex1.id) && (vertex2.id == other.vertex2.id) ||
  (vertex1.id == other.vertex2.id) && (vertex2.id == other.vertex1.id)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/graphsrb/edge.rb', line 22

def eql?(other)
  self == other
end

#to_jsonObject



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

def to_json
  {vertex1: vertex1.id, vertex2: vertex2.id, weight: weight}.to_json
end

#to_sObject



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

def to_s
  "(#{vertex1.id}, #{vertex2.id}, weight:#{weight})"
end