In Ruby, you can convert an integer to a string using the to_s
method or string interpolation.
In this example,
42
to a variable named num
.to_s
method to convert the integer num
to a string and store the result in the variable str_num
.to_s
method converts the integer to its string representation.# Convert Integer to String using to_s Method
num = 42
str_num = num.to_s
puts "String representation of integer: #{str_num}"
String representation of integer: 42
In this example,
-10
to a variable named negative_num
.negative_num
to a string and store the result in the variable str_negative_num
.# Convert Negative Integer to String using String Interpolation
negative_num = -10
str_negative_num = "String representation of negative integer: #{negative_num}"
puts str_negative_num
String representation of negative integer: -10
In this tutorial, we learned How to Convert Integer to String in Ruby language with well detailed examples.