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