How can I split an array into delimiters in Ruby?
You can probably do it with a stack, but it's pretty easy to design recursively:
#!/usr/bin/env ruby x = [:open, 1, :open, 2, 3, :close, 4, :close, :open, 5, :close] def parse(list) result =  while list.any? case (item = list.shift) when :open result.push(parse(list)) when :close return result else result.push(item) end end return result end puts parse(x).inspect
Note that this will destroy your original array. You must have
it before transferring it if you want to keep it.
source to share