Adding Variables into String in Ruby
You can always add variable into string in Ruby by concatenating variable with string via #{} notation. Example of such enclosure is the following use case
my-string = “adding string”
my-integer = 10
“My original string and additional #{my-string}. And my number is #{my-integer}”.
It is important to note in Ruby variable and string concatenation happen regardless of the data type of the variable. In the example above, you can see that my-integer was easily added to a string without any errors you may see with other languages that require you to perform casting or conversions.
Here is more complex example of such concatenation of Ruby object with the string.
%{Here is #{
class InstantClass
def bar
"some text"
end
end }.}
In case you need to escape string interpolation, you can always escape it using back slash or adding single quotes around it.
“\#{my-string}” or ‘#{my-string}’