Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
String comparision help
Live365 Live365
3/19/2009 11:22:00 PM
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.
--
Posted via
http://www.ruby-...
.
3 Answers
Tim Hunter
3/19/2009 11:30:00 PM
0
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}
> 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!
--
RMagick:
http://rmagick.ruby...
Tim Hunter
3/19/2009 11:33:00 PM
0
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.
--
RMagick:
http://rmagick.ruby...
Live365 Live365
3/20/2009 12:21:00 PM
0
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.
--
Posted via
http://www.ruby-...
.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
String comparision help
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password