That can be shortened to:
IO.foreach("testdict.txt"){ |line|
line.downcase!.chomp!
puts line if line == line.reverse?
}
Note the chomp!
Regards,
Dan
···
-----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.
Never chain bang methods:
irb(main):001:0> "hello".downcase!.chomp!
NoMethodError: private method `chomp!' called for nil:NilClass
from (irb):1
Peter
···
On Fri, 2 Sep 2005, Berger, Daniel wrote:
That can be shortened to:
IO.foreach("testdict.txt"){ |line|
line.downcase!.chomp!
puts line if line == line.reverse?
}
Note the chomp!
No, don't do that. The "bang" methods return nil when they don't change anything:
Neo:~$ cat testdict.txt
nothing
wow
Neo:~$ cat palidrome.rb
IO.foreach("testdict.txt"){ |line|
line.downcase!.chomp!
puts line if line == line.reverse?
}
Neo:~$ ruby palidrome.rb
palidrome.rb:2: private method `chomp!' called for nil:NilClass (NoMethodError)
from palidrome.rb:1:in `foreach'
from palidrome.rb:1
James Edward Gray II
···
On Sep 1, 2005, at 10:17 AM, Berger, Daniel wrote:
IO.foreach("testdict.txt"){ |line|
line.downcase!.chomp!
puts line if line == line.reverse?
}
Beware of the bang methods. They can be dangerous when chained:
a="test\n"
==>"test\n"
a.downcase!.chomp!
NoMethodError: private method `chomp!' called for nil:NilClass
from (irb):50
a
==>"test\n"
instead, use:
line=line.downcase.chomp
or
line = line.downcase.strip
... which will remove *all* surrounding whitespace, not just newlines.
cheers,
Mark
···
On 9/1/05, Berger, Daniel <Daniel.Berger@qwest.com> 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:
harp:~ > irb
irb(main):001:0> 'foobar'.downcase!
=> nil
so the code will throw like so:
irb(main):002:0> 'foobar'.downcase!.chomp!
NoMethodError: private method `chomp!' called for nil:NilClass
from (irb):2
so maybe:
harp:~ > cat a.rb
palindrome = lambda{|s| s = s.strip and s == s.reverse}
IO::foreach("words"){|w| puts w if palindrome[ w ]}
harp:~ > ruby a.rb
bib
bob
boob
civic
dad
deed
did
dud
eke
ere
ewe
eye
gag
gig
huh
level
madam
non
noon
nun
peep
pep
pip
pop
pup
radar
redder
refer
reviver
rotator
rotor
sees
sexes
solos
tit
cheers.
-a
···
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!
--
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
===============================================================================
Which can cause errors for lines that don't include new lines. A safer
version would be:
IO.foreach("testdict.txt"){ |line|
puts line if line.downcase.chomp == line.downcase.chomp.reverse
}
···
On 9/1/05, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
> 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
>
That can be shortened to:
IO.foreach("testdict.txt"){ |line|
line.downcase!.chomp!
puts line if line == line.reverse?
}
--
===Tanner Burson===
tanner.burson@gmail.com
http://tannerburson.com <---Might even work one day...