そのものズバリの instance method String#squeeze (Ruby 2.4.0) というものがありました。
In a given text, if there are two or more identical characters in sequence, delete the repetitions and leave only the first character.
Shellless mollusk lives in wallless house in wellness. Aaaarrghh! ↓ Sheles molusk lives in wales house in welnes. Aargh!
But as he spake he drew the good sword from its scabbard, and smote a heathen knight, Jusssstin of thee Iron Valley. No matttter whom you choose, she deccccclared, I will abide by your decision. Wwwhat is your will? At his magic speech the ground oppened and he began the path of descent. I should fly away and you would never see me again.
But as he spake he drew the god sword from its scabard, and smote a heathen knight, Justin of the Iron Valey. No mater whom you chose, she declared, I wil abide by your decision. Wwhat is your wil? At his magic spech the ground opened and he began the path of descent. I should fly away and you would never se me again.
- The text is case-sensitive: ‘a’ and ‘A’ are different characters.
- The input consists of 40 text lines.
- The maximum size of the text is 10 KB.
#!/usr/bin/env ruby -w ARGF.each_line do |line| puts line.chomp.split(" ").map(&:squeeze).join(" ") end