Joseph Wood
Joseph Wood

Reputation: 9

Ruby `to_i!` method not working. Wondering why

I had the following as part of a simple Ruby training program:

returns an error:

user_num = gets.chomp
user_num.to_i!

works fine:

user_num = Integer(gets.chomp)

works fine:

user_num = gets.chomp.to_i

works fine:

user_numX = gets.chomp
user_num = user_numX.to_i

Here is the program:

print "Integer please: "
#code insert location

if user_num < 0
  puts "You picked a negative integer!"
elsif user_num > 0
  puts "You picked a positive integer!"
else
  puts "You picked zero!"
end

Any ideas on why the first instance returns an error but the other three work fine? The first should work fine. It's bugging me. Thanks.

Upvotes: 0

Views: 793

Answers (1)

clandry94
clandry94

Reputation: 125

According to the Ruby Doc, the method String#to_i! doesn't exist, so that should explain why you are receiving what I presume is a NoMethodError.

Upvotes: 2

Related Questions