create a template string in ruby

In Ruby, we can create a template string using backticks (``) or the %() notation. Both methods allow us to interpolate variables into a string.

Here is an example using backticks:

main.rb
name = "John"
message = `Hello, #{name}!`
42 chars
3 lines

In this example, the value of name is interpolated into the backtick string using #{name}. The resulting value of message will be "Hello, John!".

Alternatively, we can use the %() notation to achieve the same result:

main.rb
name = "John"
message = %Q(Hello, #{name}!)
44 chars
3 lines

The %Q notation is a shorthand for a double-quoted string that allows interpolation. The resulting value of message will be "Hello, John!".

gistlibby LogSnag