On Aug 11, 2007, at 2:52 AM, Andrew Savige wrote:

> For a string "ZBBBCZZ", I want to produce a list ["Z", "BBB", "C",  
> "ZZ"]
> That is, break the string into pieces based on change of character.
>
> Though this works:
>
>   s = "ZBBBCZZ"
>   x = s.scan(/((.)\2*)/).map {|i| i[0]}

Yeah, it's short but I agree with things you dislike about it. My  
approach was essentially the same as Jeremy's;

   s.split(//).inject([]) {|g, c| (g.last && g.last[c] ? g.last : g)  
<< c; g}

That's just playing around though, I think that approach is not better.

In my view a better idiom would be to split on character switches.  
That would be concise. But as you know if you put groups you get them  
back. I see no way to express the condition for boundaries without  
using groups.

-- fxn