Skip to content

Instantly share code, notes, and snippets.

@jbfink
Created July 6, 2012 15:07
Show Gist options
  • Save jbfink/3060748 to your computer and use it in GitHub Desktop.
Save jbfink/3060748 to your computer and use it in GitHub Desktop.
monkeypatch String class for palindrome detection, strip spaces
class String
def palindrome?
junk = [",", "?", "!", ".", "'", '"', " ", "@", "#", "$", "%", "^", "&", "*"]
a = self.split("")
a.delete_if { |x| junk.include?(x) }
a.join.downcase == a.join.reverse.downcase
end
end
# 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