How to convert string object into integer?



Hi all, I'm new to ruby.
Here is the example code I was trying to test.
But it popped up an error message that like "failed to compare string object to 0".
So how to convert a string to an integer?
Thanks a lot!

print "input a integer"
x = gets.chomp
if x > 0
    print "Positive"
elsif x < 0
    print "Negative"
    print "zero"


To convert a string to an integer, use,


It is not an in-place mutation, though, so it needs to be assigned or passed as an argument in a method call expression.

y = x.to_i


y = my_method(x.to_i)


Thanks a lot, but I don't understand what is "in-place" mutation yet. :neutral_face:


An in-place mutation (insitu) is when the change (mutation) is written back onto itself.

s = "some string"
t = s.capitalize
puts t           # Some string
puts s           # some string
s.capitalize!    # notice the !
puts s           # Some string