From: dblack@... Date: 2007-08-12T20:37:10+09:00 Subject: Re: Split a string based on change of character Hi -- On Sun, 12 Aug 2007, botp wrote: > On 8/11/07, dblack@rubypal.com wrote: >>> s = "ZBBBCZZ" >>> x = s.split('').group_by{|x| x}.entries >>> >>> or possibly to >>> >>> x = s.split('').group_by.entries >> >> I'm going to have to get special glasses that can read invisible >> ink.... :-) > > whoops, sorry =) > that should be > > fr > x = s.split('').group_by{|x| x}.entries.map{|x| x.join} > > to > x = s.split('').group_by.entries.map{|x| x.join} > > i assume that group_by without a block would group the elements by > themselves. maybe i should name it group not group_by :) Actually I think group_by with nothing specified just returns an enumerator over the array itself, so it probably will never be used (I hope :-) I don't think group_by will work for this problem, though, because it groups everything together: irb(main):014:0> s => "AABCDAAE" irb(main):015:0> s.split(//).group_by {|x| x }.map {|e| e.join } => ["AAAAA", "CC", "EE", "DD", "BB"] Notice how all the A's got put in one result. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)