RosettaCodeData/Task/Run-length-encoding/Ruby/run-length-encoding-4.rb

8 lines
127 B
Ruby

def encode(str)
str.gsub(/(.)\1*/) {$&.length.to_s + $1}
end
def decode(str)
str.gsub(/(\d+)(\D)/) {$2 * $1.to_i}
end