Andrew Savige schrieb: > 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]} you may want to write it as ...map{|i,|i} > 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? No idea, here is another variant to play with: x = /#{s.gsub(/(.)\1*/, '(\1+)')}/.match(s).captures funny little problem. cheers Simon