On Aug 10, 7:52 pm, Andrew Savige <ajsav... / yahoo.com.au> 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]} > > I'm new to Ruby and am interested to learn if there is a better way to > do it. > > BTW, in Python, it can be done with a regex (similar to above) or via > their itertools library: > > import itertools > s = "ZBBBCCZZ" > x = [''.join(g) for k, g in itertools.groupby(s)] > > Does anyone know if Ruby has a similar library to Python's itertools? > > Thanks, > /-\ > > ____________________________________________________________________________________ > Sick of deleting your inbox? Yahoo!7 Mail has free unlimited storage.http://au.docs.yahoo.com/mail/unlimitedstorage.html s = "ZBBBCZZ" ==>"ZBBBCZZ" s.scan( /((.)\2*)/ ).transpose.first ==>["Z", "BBB", "C", "ZZ"] s.gsub( /(.)(?!\1)/, "\\1\n" ).split ==>["Z", "BBB", "C", "ZZ"]