Skip to main content

How to Write a Single-Quoted String in Ruby

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Ruby treats single and double quotes differently. In double quotes, #{name} is replaced by the value of name and \n becomes a line break. In single quotes, both stay exactly as written, which is what you want for JSON and other text full of " or #.

Syntax​

Syntax
'<text, "double quotes" and #{braces} as they are>'
'<it\'s with an escaped quote and a backslash: \\>'
PartWhat it does
'...'A single-quoted string. No interpolation, and no escape sequences except two.
\'A single quote inside the string.
\\A backslash.

The Ruby documentation on string literals: "In addition to disabling interpolation, single-quoted strings also disable all escape sequences except for the single-quote (\') and backslash (\\). In a single-quoted string, any other character following a backslash is interpreted as is: a backslash and the character itself."

The Example​

The example prints the same greeting twice, once in single quotes and once in double quotes, then a piece of JSON in single quotes, and a string with an escaped single quote and a backslash.

Prerequisites​

  • Ruby. This guide uses Ruby 4.0.10.

Step 1: Write the Code​

Create a file named main.rb:

main.rb, which sets name to Ada and puts a single-quoted greeting with interpolation and a backslash n, the same greeting in double quotes, JSON in single quotes, and It's a backslash with an escaped quote and backslash.

main.rb
name = 'Ada'

puts 'Hello, #{name}\n'
puts "Hello, #{name}"
puts '{"name":"Ada","role":"admin"}'
puts 'It\'s a backslash: \\'

Step 2: Run It​

Terminal
ruby main.rb
Output
Hello, #{name}\n
Hello, Ada
{"name":"Ada","role":"admin"}
It's a backslash: \

A zsh terminal where ruby main.rb prints the single-quoted greeting with its hash, braces and backslash n literally, then Hello, Ada, the JSON, and It's a backslash: .

The command is the same in zsh, bash and PowerShell.

What Happened​

  • In single quotes, the interpolation and the \n came out as those characters.
  • In double quotes, the interpolation became Ada.
  • The JSON kept its double quotes with no escaping.
  • \' gave a single quote and \\ a single backslash.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting interpolation in single quotesThe braces and the name are printed as they areUse double quotes
Writing '\n' for a line breakIt prints a backslash and an nUse "\n"
An unescaped single quote, as in 'It's'A syntax error: unexpected local variable or method, expecting end-of-inputWrite 'It\'s', or use double quotes

Troubleshooting​

SymptomLikely causeFix
Interpolation braces appear in the outputThe string is in single quotesSwitch to double quotes
A syntax error near an apostropheThe apostrophe ended a single-quoted stringEscape it, or use double quotes

FAQ​

Which should I use by default?​

Either; many Ruby style guides prefer single quotes unless the string needs interpolation or escape sequences. Choose by what the text contains.

Are there other ways to write a string with both kinds of quote?​

Yes: %q(It's "fine") works like single quotes and %Q(...) like double quotes, so neither kind of quote needs escaping.

Conclusion​

A single-quoted Ruby string keeps its text exactly as written, apart from \' and \\. That makes it the right choice for JSON and anything full of " or #, and the wrong one when you need interpolation or a line break.

Additional Resources​