Class: Graphsrb::Edge
- Inherits:
-
Object
- Object
- Graphsrb::Edge
- Defined in:
- lib/graphsrb/edge.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#vertex1 ⇒ Object
(also: #initial_vertex)
readonly
Returns the value of attribute vertex1.
-
#vertex2 ⇒ Object
(also: #terminal_vertex)
readonly
Returns the value of attribute vertex2.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(id1, id2, args = {}) ⇒ Edge
constructor
A new instance of Edge.
- #to_json ⇒ Object
- #to_s ⇒ Object
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
#vertex1 ⇒ Object (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 |
#vertex2 ⇒ Object (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 |
#weight ⇒ Object (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
22 23 24 |
# File 'lib/graphsrb/edge.rb', line 22 def eql?(other) self == other end |
#to_json ⇒ Object
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_s ⇒ Object
26 27 28 |
# File 'lib/graphsrb/edge.rb', line 26 def to_s "(#{vertex1.id}, #{vertex2.id}, weight:#{weight})" end |