Created
March 30, 2009 08:39
-
-
Save scrogson/87688 to your computer and use it in GitHub Desktop.
Add a palidrome? method to the Ruby String ojbect. Completely pointless.
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? | |
self.gsub!(/[[:punct:][:space:]]/, '') | |
self.downcase == self.downcase.reverse | |
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
require File.join(File.dirname(__FILE__), '../palindrome') | |
describe String do | |
it "should have a palindrome? method" do | |
"String".should respond_to :palindrome? | |
end | |
it "should acknowledge a valid palindrome" do | |
valid_palindromes = [ | |
"Madam, I'm Adam.", | |
"Do geese see God?", | |
"Was it Eliot's toilet I saw?", | |
"Murder for a jar of red rum." | |
] | |
valid_palindromes.each do |p| | |
p.palindrome?.should be_true | |
end | |
end | |
it "should reject non-valid palindromes" do | |
"blah blah blah".palindrome?.should be_false | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment