OneBite.Dev - Coding blog in a bite size

check if a string is a palindrome in Ruby

Code snippet on how to check if a string is a palindrome in Ruby

def palindrome?(string)
  string == string.reverse
end

This code checks if a string is a palindrome. It defines a new method called ‘palindrome?’, which takes a string argument. The method then compares if the string is equal to the reverse of itself. If the string and its reverse are the same, then it is a palindrome. If not, then it’s not a palindrome. This method returns a boolean value (true if palindrome, false if not). For example, when checking if the string “racecar” is a palindrome, the reversed string would be “racecar” – which is the same – so the result would be true.

ruby