# String comparision help

**URL:** <https://www.ruby-forum.com/t/string-comparision-help/163406>\
**Category:** Ruby\
**Created:** [March 20, 2009, 12:25am UTC](https://www.ruby-forum.com/t/string-comparision-help/163406 "2009-03-20T00:25:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![live365](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@live365](https://www.ruby-forum.com/u/live365)\
**Post date:** [March 20, 2009, 12:25am UTC](https://www.ruby-forum.com/t/string-comparision-help/163406/1 "2009-03-20T00:25:00Z")

</div>

Hi,

I am new to ruby. I am trying to get input from the user and then trying  
to compare that string to perform action depending on the comparision  
result:  
Here is the code  
while command = STDIN.gets  
command.chomp  
print #{command}  
if command == “add”  
addnums(num1,num2)  
break  
else  
print"not correct command"  
end  
end

When I run this code and enter add it is giving output : Not the right  
command.

Was expecting method addnums be called. because command is add which is  
a string and if statement to evaluate to be true.

---

<div class="post-metadata">

**Author:** ![live365](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@live365](https://www.ruby-forum.com/u/live365)\
**Post date:** [March 20, 2009, 12:35am UTC](https://www.ruby-forum.com/t/string-comparision-help/163406/2 "2009-03-20T00:35:56Z")

</div>

Live365 Live365 wrote:

> Hi,
> 
> I am new to ruby. I am trying to get input from the user and then trying  
> to compare that string to perform action depending on the comparision  
> result:  
> Here is the code  
> while command = STDIN.gets  
> command.chomp  
> print #{command}

Forgot to add, if you use the p method to print the command instead of  
print, you would have seen the newline character. p is good for  
debugging.

p command

Notice that you don’t have to use #{}, either.

---

<div class="post-metadata">

**Author:** ![live365](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@live365](https://www.ruby-forum.com/u/live365)\
**Post date:** [March 20, 2009, 12:33am UTC](https://www.ruby-forum.com/t/string-comparision-help/163406/3 "2009-03-20T00:33:35Z")

</div>

Live365 Live365 wrote:

> ```
> addnums(num1,num2)
> 
> ```
> 
> a string and if statement to evaluate to be true.  
> command.chomp does not change command, it returns a new string, which  
> this code does not use. You could use this instead:

command = command.chomp

or just change command like this:

command.chomp!

---

<div class="post-metadata">

**Author:** ![live365](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@live365](https://www.ruby-forum.com/u/live365)\
**Post date:** [March 20, 2009, 1:23pm UTC](https://www.ruby-forum.com/t/string-comparision-help/163406/4 "2009-03-20T13:23:34Z")

</div>

Tim H. wrote:

> Live365 Live365 wrote:
> 
> > Hi,
> > 
> > I am new to ruby. I am trying to get input from the user and then trying  
> > to compare that string to perform action depending on the comparision  
> > result:  
> > Here is the code  
> > while command = STDIN.gets  
> > command.chomp  
> > print #{command}
> 
> Forgot to add, if you use the p method to print the command instead of  
> print, you would have seen the newline character. p is good for  
> debugging.
> 
> p command
> 
> Notice that you don’t have to use #{}, either.

Thanks Tim it worked. p command is also very useful to see what is the  
exact return.
