Skip to content

Instantly share code, notes, and snippets.

@bgnori
Created April 29, 2012 17:57
Show Gist options
  • Save bgnori/2552264 to your computer and use it in GitHub Desktop.
Save bgnori/2552264 to your computer and use it in GitHub Desktop.
s = "ab[cd]ef"
expected = ['a', 'b', ['c', 'd' ], 'e', 'f']
def parse(xs)
result = []
while not xs.empty?
x = xs[0]
xs.slice!(0, 1)
if x == '['
got = parse(xs)
result.push(got)
elsif x == ']'
return result
else
result.push(x)
end
end
return result
end
p parse("[][]")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment