[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem with string comparison

Lucas Holland

12/19/2006 4:07:00 PM

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

--
Posted via http://www.ruby-....

3 Answers

Brad Phelan

12/19/2006 4:12:00 PM

0

Lucas Holland 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
>

Try

> name = gets.chomp

hemant

12/19/2006 4:17:00 PM

0

On 12/19/06, Lucas Holland <hollandlucas@gmail.com> 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,

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.


--
gnufied
-----------
There was only one Road; that it was like a great river: its springs
were at every doorstep, and every path was its tributary.

Lucas Holland

12/19/2006 6:02:00 PM

0

Lucas Holland 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

--
Posted via http://www.ruby-....