Problem with string comparison

L

Lucas Holland

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
 
B

Brad Phelan

Lucas said:
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

Try

name = gets.chomp
 
H

hemant

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,

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.
 
L

Lucas Holland

Lucas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top