Created
August 13, 2014 16:14
-
-
Save iaintshine/49eab7d345a280ef7d0c to your computer and use it in GitHub Desktop.
A Ruby program which checks command line arguments if they are palindromes
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
#!/usr/bin/env ruby | |
# palindrome.rb | |
module Palindrome | |
ALPHABET = ('a'..'z').to_a + ('A'..'Z').to_a | |
def palindrome? | |
normalized == normalized.reverse | |
end | |
def normalized | |
self.split('').select { |char| ALPHABET.include? char }.join('').downcase | |
end | |
end | |
class String | |
include Palindrome | |
end | |
ARGV.each do |sentence| | |
puts %Q{sentence "#{sentence}" is #{ sentence.palindrome? ? '' : 'not' } palindrome} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment