Skip to content

Instantly share code, notes, and snippets.

@iaintshine
Created August 13, 2014 16:14
Show Gist options
  • Save iaintshine/49eab7d345a280ef7d0c to your computer and use it in GitHub Desktop.
Save iaintshine/49eab7d345a280ef7d0c to your computer and use it in GitHub Desktop.
A Ruby program which checks command line arguments if they are palindromes
#!/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