From: James Edward Gray II Date: 2005-09-02T00:22:17+09:00 Subject: Re: palindrome finder On Sep 1, 2005, at 10:06 AM, Josh Charles wrote: > I've been working on this piece of code and it's starting to drive me > crazy. No worries, it's a simple mistake with an easy fix. The Problem: Each line read has a "\n" at the end of it, which is making your test fail, because "wow\n" does equal "\nwow". The fix: Strip the newline, like so > Here is my code: > > file = File.open("testdict.txt", "r" ) do |file| > file.each_line("\n") do |line| > #filelist.push( line ) line.strip! # removes all whitespace at the front and back of the line > line.downcase! > if (line == line.reverse) > puts line > end > end > end Just FYI, you can also simplify the above a little. Here's an example: Neo:~$ cat testdict.txt nothing wow Neo:~$ cat palidrome.rb ARGF.each_line do |line| line.strip! line.downcase! puts line if line == line.reverse end Neo:~$ ruby palidrome.rb testdict.txt wow Hope that helps. James Edward Gray II