Choi, Junegunn
2/11/2009 11:53:00 AM
Hi, this is my code just for unfolding braces in a string. It also
tries to handle nested braces like Dir.glob. Seems working fine.
def unfold(string)
def recursion(input, list)
num_groups = 0
input.scan(/\{([^{}]*?)\}/) do | group |
prefix, suffix = $`, $'
num_groups += 1
tokens = group[0].split(/[,|]/)
tokens = [''] if tokens.empty?
tokens.each do | token |
recursion(prefix + token + suffix, list)
end
break
end
list << input if num_groups == 0
end
list = []
recursion(string, list)
list.uniq
end
puts unfold("This {is|was} a {good|nice} day")
# A more complex one
puts unfold('Who is {th{is,at},she,he,it} {?,!}{}')