← Back to Ruby Operators

Double-Quoted Strings

A pair of double quotes ("") in Ruby is used to define a string that supports interpolation and escaped characters like \n.

> "Hello"
=> "Hello"
> who = "World"
=> "World"
> "Hello, #{who}!"
=> "Hello, World!"

If the string we are defining should contain a ", then we have to escape that character for it to parse correctly.

> "Strings are wrapped in a \" character."
=> "Strings are wrapped in a \" character."

Here we can see that double-quoted strings support escaped characters like \n where as single-quoted strings do not.

> puts "1. One\n2. Two"
1. One
2. Two
=> nil
> puts '1. One\n2. Two'
1. One\n2. Two
=> nil

We can also use double-quotes with the : syntax to define a symbol from a string. As you might expect, this too supports interpolation.

> :"file"
=> :file
> i = 0
=> 0
> :"file_copy(#{i += 1})"
=> :"file_copy(1)"
> :"file_copy(#{i += 1})"
=> :"file_copy(2)"
> :"file_copy(#{i += 1})"
=> :"file_copy(3)"

References