Class: Regexgen::State

Inherits:
Object
  • Object
show all
Defined in:
lib/regexgen/state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Returns a new instance of State.



8
9
10
11
# File 'lib/regexgen/state.rb', line 8

def initialize
  @accepting = false
  @transitions = Hash.new { |hash, key| hash[key] = State.new }
end

Instance Attribute Details

#acceptingObject

Returns the value of attribute accepting.



5
6
7
# File 'lib/regexgen/state.rb', line 5

def accepting
  @accepting
end

#transitionsObject (readonly)

Returns the value of attribute transitions.



6
7
8
# File 'lib/regexgen/state.rb', line 6

def transitions
  @transitions
end

Instance Method Details

#to_hObject



21
22
23
24
25
# File 'lib/regexgen/state.rb', line 21

def to_h
  @transitions.transform_values(&:to_h).tap do |h|
    h[''] = nil if @accepting
  end
end

#to_sObject Also known as: inspect



27
28
29
30
# File 'lib/regexgen/state.rb', line 27

def to_s
  sigil = @accepting ? '*' : ''
  "#{sigil}#{to_h}"
end

#visit(visited = Set.new) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/regexgen/state.rb', line 13

def visit(visited = Set.new)
  return visited if visited.include?(self)

  visited.add(self)
  @transitions.each_value { |state| state.visit(visited) }
  visited
end