# Problem with string comparison

**URL:** <https://www.ruby-forum.com/t/problem-with-string-comparison/79834>\
**Category:** Ruby\
**Created:** [December 19, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/problem-with-string-comparison/79834 "2006-12-19T17:06:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [December 19, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/problem-with-string-comparison/79834/1 "2006-12-19T17:06:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [December 19, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/problem-with-string-comparison/79834/2 "2006-12-19T19:02:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [December 20, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/problem-with-string-comparison/79834/3 "2006-12-20T19:24:56Z")

</div>

On 12/19/06, Lucas H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![chell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/73ab20/32.png) [@chell](https://www.ruby-forum.com/u/chell)\
**Post date:** [December 20, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/problem-with-string-comparison/79834/4 "2006-12-20T19:08:11Z")

</div>

On 12/19/06, Lucas H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
