Class: PatternStringGenerator
- Inherits:
-
Object
- Object
- PatternStringGenerator
- Defined in:
- lib/pattern_string_generator.rb
Instance Method Summary collapse
-
#initialize(pattern) ⇒ PatternStringGenerator
constructor
A new instance of PatternStringGenerator.
- #to_s ⇒ Object
Constructor Details
#initialize(pattern) ⇒ PatternStringGenerator
Returns a new instance of PatternStringGenerator.
2 3 4 |
# File 'lib/pattern_string_generator.rb', line 2 def initialize(pattern) @pattern = pattern end |
Instance Method Details
#to_s ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pattern_string_generator.rb', line 6 def to_s stack = [] stack_depth = 0 @pattern.scan(/\(|\)|\||[^\(\)\|]+/) do |token| # puts "stack is: #{stack.inspect}, token is: #{token.inspect}" case token when '(' stack_depth += 1 stack.push '(' when ')' stack_depth -= 1 if stack_depth < 0 raise "Unbalanced brackets" end close_bracket(stack) when '|' stack.push '|' else stack.push token end end stack.join end |