Palindrome finder

Yes, I've been corrected, 4 times now. Thanks.

Dan

···

-----Original Message-----
From: Ara.T.Howard [mailto:Ara.T.Howard@noaa.gov]
Sent: Thursday, September 01, 2005 9:32 AM
To: ruby-talk ML
Subject: Re: palindrome finder

On Fri, 2 Sep 2005, Berger, Daniel wrote:

>> -----Original Message-----
>> From: Josh Charles [mailto:josh.charles@gmail.com]
>> Sent: Thursday, September 01, 2005 9:06 AM
>> To: ruby-talk ML
>> Subject: palindrome finder
>>
>>
>> I've been working on this piece of code and it's starting
to drive me
>> crazy. I think it's a problem with reference vs value
type, but I'm
>> not sure. I'm just writing a simple program to process a list of
>> words and print out all the palindromes (words that read the same
>> forward and backward)
>>
>> Here is my code:
>>
>> file = File.open("testdict.txt", "r" ) do |file|
>> file.each_line("\n") do |line|
>> #filelist.push( line )
>> line.downcase!
>> if (line == line.reverse)
>> puts line
>> end
>> end
>> end
>>
>> I've made sure each line is being read properly, and it is. The if
>> statement never returns true, however. I'm new to ruby, so I'm
>> probably just doing something really stupid, but I can't figure it
>> out yet.
>
> That can be shortened to:
>
> IO.foreach("testdict.txt"){ |line|
> line.downcase!.chomp!
> puts line if line == line.reverse?
> }
>
> Note the chomp!

rule of thumb - never chain bang methods:

don't worry - it's happened to us all... i didn't read the others first btw!

:wink:

cheers.

-a

···

On Fri, 2 Sep 2005, Berger, Daniel wrote:

rule of thumb - never chain bang methods:

Yes, I've been corrected, 4 times now. Thanks.

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================