Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s = "{new {value1 value2 value3}} {old {value2 value1 value1}} {{old school} {value2 value3 value1}}"
- os = []
- level = 0
- pos = 0
- str = Struct.new(:key, :values)
- value = ""
- o = str.new("", [])
- for i in 0...s.size
- x = s[i...i+1]
- case x
- when "{"
- level += 1
- when "}"
- if level == 2
- pos = 1
- end
- level -= 1
- when " "
- case level
- when 0
- o.values << value
- os << o.dup
- o = str.new("", [])
- value = ""
- pos = 0
- when 1
- if pos == 0
- pos = 1
- else
- value = ""
- end
- when 2
- if pos == 0
- o.key += x
- else
- o.values << value.dup
- value = ""
- end
- else
- raise
- end
- else
- if pos == 0
- o.key += x
- else
- value += x
- end
- end
- end
- o.values << value
- os << o
- puts os.inspect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement