Class: Regexgen::Ast::Alternation

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

Overview

Represents an alternation (e.g. ‘foo|bar`)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Alternation

Returns a new instance of Alternation.



10
11
12
13
# File 'lib/regexgen/ast.rb', line 10

def initialize(*options)
  @precedence = 1
  @options = flatten(options).sort { |a, b| b.length - a.length }
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#precedenceObject (readonly)

Returns the value of attribute precedence.



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

def precedence
  @precedence
end

Instance Method Details

#lengthObject



15
16
17
# File 'lib/regexgen/ast.rb', line 15

def length
  @options[0].length
end

#to_sObject



19
20
21
# File 'lib/regexgen/ast.rb', line 19

def to_s
  @options.map { |o| Ast.parens(o, self) }.join('|')
end