Grasping `all-the-apples-at-once' › 解法样本 Sample solutions › Ruby (Group I) [#278]

Ruby 有三种解法⸺都是单行的,而且很相似。第一个可能是最容易理解的。
There were three solutions in Ruby -- all one-liners, and rather similar. The first is probably the easiest to understand.

"aaaabbbcca".split("").chunk_while {|a,b| a==b}.map {|a| [a.first, a.size]}
"aaaabbbcca".chars.slice_when {|c,n| c != n }.map {|s| [s[0], s.size]}
'aaaabbbcca'.chars.chunk(&:itself).map{|x,y| [x, y.size]}