#8 Palindromes (palindrome.rb and palindrome2.rb)

I’m rounding out this chapter with a few shorter examples pertaining to palindromes, which are bits of text that are the same when reversed as they are when read normally. Usually, we allow cheating to ignore spaces, capitalization differences, and punctuation, so A man, a plan, a canal, Panama qualifies as a palindrome under those conditions. While working on this book, I was reading another programming book that contained a discussion about palindromes. “Great!” I thought. “I’ll add a palindrome? predicate method to all Strings. It’ll be a nice simple bit to have in the chapter in which I talk about text.” So I started thinking about breaking Strings into individual characters, writing a method ...

Get Ruby by Example now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.