4. String

4.9 Add number to string using to_s

I’m printing the age of Ruby in this example.



If I try to split the puts statement then I will get an error. Execute the below code and see the error for yourself.



The error message says that no implicit conversion of Integer into String.

In simple english, Ruby is trying to say that we cannot add an integer with a string.

If we want to add an integer to a string then first we need to convert the integer to a string. Ruby provides method to_s to convert an integer to string. to_s stands for “to string”. Let’s use to_s this time.

Now let’s run the code.