If statements and 'gets.chomp'-variables


Link to exercise (it's a review lesson where i should make something i wanted)

I'm not exactly sure, can i, as i have done in the code, set a variable to 'gets.chomp' and then use it in an if-elsif-else statement. Please tell me if there is something wrong with my code:

print "Type an integer and the program will configure out wether it is positive, negative, or just 0:"

variable = gets.chomp

if variable > 0
    print "Your number is a positive integer"
elsif varible < 0
    print "Your number is a negative integer"
    print "Your number is 0"

It also outputs this error message:
comparison of String with 0 failed

I don't understand that error message. Can you help me?


Check typo in elsif clause.

gets take an user input and saves as a string, You need to convert it in number.
use .to_i or Integer().


Could you show me exactly how the syntax is (how to do and where to put it)?


We learnt that we can chain our methods , right ?

It can be like this..

gets.chomp.Method_I_mentioned ( to_i)

or you can use Integer() over your input.


I don't encourage it's use but in Ruby you can use the global variable $_ to store the last gets


case $_.downcase.chomp

if X then Y
if foo then bar
etc etc etc


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.