to_i Method in Ruby


Q: What to_i stands for in Ruby and what it does?
A: to_i stands for “to integer” and is needed to convert strings into integers so you can do mathematical operations with them. Any value, even numbers, acquired from URLs in params becomes a string and if you need to do some math with it you need to convert it to integer.

..
get '/what/time/is/it/in/:number/hours' do
number = params[:number].to_i
time = Time.now + number * 3600
"The time in #{number} hours will be #{time.strftime('%I:%M %p')}"
end

The number you enter will become a string, to calculate time and give correct answer you need to make it integer. That’s where to_i comes in.

More: http://apidock.com/ruby/String/to_i