OneBite.Dev - Coding blog in a bite size

convert variable from int to string in Ruby

Code snippet on how to convert variable from int to string in Ruby

a = 9
b = a.to_s

In this code, we are taking the integer variable “a” and setting it to the value 9. We then take the variable “a” and call the method “.to_s” on it, which will convert the variable’s value from an integer to a string. We then save this new value in the variable “b”. So, the value of variable “a” has been converted from an integer 9 to a string “9” and stored in variable “b”.

ruby