To convert a String to an Integer in Ruby, you can use the to_i
method.
In this example,
str
with a string value of "222"
.to_i
method to convert it to an integer and store the result in the variable number
.to_i
method converts the string to an integer.number
using puts
.str = "222"
number = str.to_i
puts "Integer value: #{number}"
Integer value: 222
In this example,
str
with a string value of "888"
.to_i
method to convert it to an integer and store the result in the variable number
.to_i
method converts the string to an integer.number
using puts
.str = "888"
number = str.to_i
puts "Integer value: #{number}"
Integer value: 888
In this tutorial, we learned How to Convert String to Integer in Ruby language with well detailed examples.