class String

Public Instance Methods

palindrome?() click to toggle source
# File lib/palindrome.rb, line 2
def palindrome?

  if self.nil? #self can't be null, neverthless validate it
    abort "Something went wrong, string input is nil"
  end

  text = self.scan(/[[:alnum:]]/).join.downcase #only get alphanumeric chars

  if text.empty?
    return false
  end

  is_palindrome = true
  char_array = text.split ""

  while char_array.length > 1 && is_palindrome
    is_palindrome = char_array.pop == char_array.shift
  end

  is_palindrome
end