String comparision help

L

Live365 Live365

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

Tim Hunter

Live365 said:
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.

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!
 
T

Tim Hunter

Live365 said:
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.
 
L

Live365 Live365

Tim said:
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.
 

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,144
Latest member
KetoBaseReviews
Top