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

8 lines
127 B
Ruby
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
def encode(str)
str.gsub(/(.)\1*/) {$&.length.to_s + $1}
end
def decode(str)
str.gsub(/(\d+)(\D)/) {$2 * $1.to_i}
end