Forum: Ruby String comparision help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
69ee0e262cea72367fffca746b6c6f72?d=identicon&s=25 Live365 Live365 (live365)
on 2009-03-20 00:25
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.
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-03-20 00:33
(Received via mailing list)
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!
3afd3e5e05dc9310c89aa5762cc8dd1d?d=identicon&s=25 Tim Hunter (Guest)
on 2009-03-20 00:35
(Received via mailing list)
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.
69ee0e262cea72367fffca746b6c6f72?d=identicon&s=25 Live365 Live365 (live365)
on 2009-03-20 13:23
Tim Hunter 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.
This topic is locked and can not be replied to.