From: Martin DeMello Date: 2011-12-13T19:19:37+09:00 Subject: Re: "aaabbccccdadd" => [ [a, 3], [b, 2], [c, 4], [d, 1], [a, 1], [d, 2] ] you can use the enumerator form of scan (ruby1.9): "aaabbccccdadd".scan(/((.)\2*)/).map { |a,b| [b, a.size] } martin On Tue, Dec 13, 2011 at 2:08 AM, Adit Cahya Ramadhan Adit wrote: > I'm thinking to translate string to an array like this: > "aaabbccccdadd" => [ [a, 3], [b, 2], [c, 4], [d, 1], [a, 1], [d, 2] ] > > this is my code: > > ary = [] > "aaabbccccdadd".scan(/((.)\2*)/){ |a,b| ary << [b, a.size] } > > ary #=> [["a", 3], ["b", 2], ["c", 4], ["d", 1], ["a", 1], ["d", 2]] > > But, I wonder if there are more effective or beautiful solution than > this. > How do you think? > > -- > Posted via http://www.ruby-forum.com/. >