Created
July 6, 2012 15:07
-
-
Save jbfink/3060748 to your computer and use it in GitHub Desktop.
monkeypatch String class for palindrome detection, strip spaces
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 palindrome? | |
junk = [",", "?", "!", ".", "'", '"', " ", "@", "#", "$", "%", "^", "&", "*"] | |
a = self.split("") | |
a.delete_if { |x| junk.include?(x) } | |
a.join.downcase == a.join.reverse.downcase | |
end | |
end |
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
# Doing this in a inherited class which is the Proper Grown Up Way to do it. | |
class Word < String | |
def palindrome? | |
junk = [",", "?", "!", ".", "'", '"', " ", "@", "#", "$", "%", "^", "&", "*"] | |
a = self.split("") | |
a.delete_if { |x| junk.include?(x) } | |
a.join.downcase == a.join.reverse.downcase | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment