class PatternRandomizer

Public Class Methods

new(pattern) click to toggle source
# File lib/pattern_randomizer.rb, line 2
def initialize(pattern)
  @pattern = pattern
end

Public Instance Methods

to_s() click to toggle source
# File lib/pattern_randomizer.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

Private Instance Methods

close_bracket(stack) click to toggle source
# File lib/pattern_randomizer.rb, line 34
def close_bracket(stack)
  word = stack.pop

  words = ['']

  until word == '('
    if word == '|'
      words << ''
    else
      words[words.length-1] = word + words.last
    end
    word = stack.pop
  end

  stack.push words.sample
end