Ruby: splitting a string without removing separators

ruby, split, string

Solution

Maybe something like this:

str.scan(/\(|\)|\w+/)

Problem

I need to split a string without removing the separators. Is there a simple and "Ruby oriented" way to do this? For example, given a string like this: ``` str = "(This is (a test))" ``` what I need is this: ``` ["(", "This", "is", "(", "a", "test", ")", ")"] ``` I tried using the split method for strings, using the brackets `"("` and `")"` as separators, but then I get them removed from the returning array. Any advice will be helpful.

Original source