Problem with string comparison

Hi!

The following code:

name = gets.comp.to_s
if name == “Bill” or name == “Steve”
puts “Wow, you’re pretty smart!”
end

yields this error, once executed:

NoMethodError: undefined method ‘comp’ for “Bill”:String

What is wrong with the code?

Thanks in advance,

chell

Lucas H. wrote:

Hi!

The following code:

name = gets.comp.to_s
if name == “Bill” or name == “Steve”
puts “Wow, you’re pretty smart!”
end

yields this error, once executed:

NoMethodError: undefined method ‘comp’ for “Bill”:String

What is wrong with the code?

Thanks in advance,

chell

I’ve found the problem. Apparently, you can’t explicitly convert
gets.chomp to a string (it is a string anyways, eh?), so name =
gets.chomp will do fine!

chell

On 12/19/06, Lucas H. [email protected] wrote:

I’ve found the problem. Apparently, you can’t explicitly convert
gets.chomp to a string (it is a string anyways, eh?), so name =
gets.chomp will do fine!

Um sure you can. Its pointless but you can. Your problem was, as many
other
people pointed out, that you mispelled “chomp” as “comp”.

chell

On 12/19/06, Lucas H. [email protected] wrote:

NoMethodError: undefined method ‘comp’ for “Bill”:String

What is wrong with the code?

Thanks in advance,

Because comp method is not defined for String class, probably what you
want is chomp and you don’t need a to_s in the end.