Created
June 16, 2013 07:36
-
-
Save Altech/5791289 to your computer and use it in GitHub Desktop.
squeeze in Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def squeeze(c) | |
s = "" | |
now = false | |
0.upto(self.size-1) do |i| | |
if self[i] == c | |
if now | |
next | |
else | |
s << self[i] | |
now = true | |
end | |
else | |
s << self[i] | |
now = false | |
end | |
end | |
return s | |
end | |
def squeeze2(c) | |
self.gsub(/#{c}+/,c) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment