#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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.