Class: Society::Edge

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

Overview

The Edge class represents an edge between two nodes in a graph. An edge is assumed to represent a direct relationship between two Classes or Modules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to:, weight: 1) ⇒ Edge

Public: Create a new Edge.

to - Node to target. weight - Weight of the edge, representing the number of references to the

node referenced.  (Default: 1)


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

def initialize(to:, weight: 1)
  @to     = to
  @weight = weight
end

Instance Attribute Details

#toObject (readonly)

Returns the value of attribute to.



7
8
9
# File 'lib/society/edge.rb', line 7

def to
  @to
end

#weightObject (readonly)

Returns the value of attribute weight.



7
8
9
# File 'lib/society/edge.rb', line 7

def weight
  @weight
end

Instance Method Details

#+(edge) ⇒ Object

Public: Add two Edges’ weights, returning a new Edge.

edge - An Edge.

Returns a new Edge if both edges target the same node. Returns nil otherwise.



25
26
27
28
29
# File 'lib/society/edge.rb', line 25

def +(edge)
  return nil unless edge.to == to

  Edge.new(to: to, weight: weight + edge.weight)
end

#to_sObject Also known as: inspect

Public: Return the name of the node to which the edge points.

Returns a string.



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

def to_s
  to.to_s
end