What is the difference between single and double quotes in Ruby strings?
Sorry if this is a silly one. I am on attempt number 4 of chapter 3 and I keep mixing up the quote marks. I used to set type for a living, so I like tidy punctuation, and I do not understand why Ruby has two kinds.
Cite
Poignant Guide Ruby Q&A, "What is the difference between single and double quotes in Ruby strings?", https://poignantguide.net/answers/read/what-is-the-difference-between-single-and-double-quotes-in-ruby/, accessed 2026-11-29.I tried this in a file called
quotes.rb:name = 'Hugo' puts 'Hello, #{name}' puts "Hello, #{name}" puts 'Line one\nLine two' puts "Line one\nLine two"And this is what I got:
Hello, #{name} Hello, Hugo Line one\nLine two Line one Line twoSo the single quotes printed my code instead of my name, and the
\ndid not break the line. Attempt 1 I thought I had a typo, attempt 2 I retyped the file, attempt 3 I changed#to@(that was a bad idea). I am on Ruby 3.3 on Windows. Which one should I use, and when does it matter?
Write something first.
Give people something to work with: at least 30 words on what happened and what you tried.
That is too long. Keep it under 6,000 characters.
Write the question as the title, 15 to 140 characters, no links.
Pick a category.
Add a name (2 to 40 characters, no links).
That email address does not look right.
That was quick. Read the thread, then try again.
The form expired. Reload the page and post again.
Something went wrong with the form. Reload and try again.
Please complete the check and post again.
Limit reached for now. Try again later.
This thread is closed to new replies.
This thread no longer accepts replies.
Something went wrong with the form. Reload and try again.
Post a reply or question first (name and email), then this browser can vote, edit and accept answers.
You cannot vote on your own post.
Only the author (within 30 days) or the forum team can do that.
That email already has an account. Use your sign-in link instead.
New accounts are paused for the moment. Try again later.
That was already posted.
2 answers
- Wes O.Member ·
Dry observation from the COBOL years: a delimiter that changes the meaning of the contents is a feature, until it is a bug. Practical bit for you, Hugo: a single-quoted string can hold double quotes without any escaping.
'She said "hi"'is fine, and"It's fine"is fine as well.