Vijay Shanker Dubey
Vijay Shanker Dubey

Reputation: 4418

How to add escape sequence to String variables?

When adding one escape character to string and printing the output does not replace escape character in the output string

indent = '\t'
message = 'Hello there'
message = "#{indent} #{message}"
puts(message)

From the above code output printed is as below

\t Hello there

What is wrong with this code? What is the way to achieve the intended output?

Upvotes: 1

Views: 145

Answers (1)

You have to use " instead ' in:

indent = '\t'

Look:

irb(main):001:0> indent = '\t'
=> "\\t"
irb(main):002:0> indent = "\t"
=> "\t"

Upvotes: 4

Related Questions