juniorRuby

What is the difference between single quotes and double quotes for String literals?

Updated May 17, 2026

Short answer

Double quotes support string interpolation (#{variable}) and escape sequences (like \n). Single quotes treat text almost completely literally, ignoring escape characters and interpolation.

Deep explanation

Double-quoted strings perform a preprocessing pass where they evaluate expressions wrapped in #{} and replace escape sequences like \t (tab) or \n (newline) with their control characters. Single-quoted strings skip this processing, meaning \n will be rendered literally as a backslash followed by the letter 'n'. The only characters escaped in single quotes are \\ and \'. Because single quotes bypass evaluation, they historically offered a tiny performance gain, though modern Ruby engines optimize this extensively.

Real-world example

Use double quotes when constructing dynamic messages or layouts requiring tabs or line breaks. Use single quotes for static string configuration values or fixed dictionary lookups to ensure intent clarity.

Common mistakes

  • Using single quotes and wondering why string interpolation is printed out as text instead of rendering the variable.

Follow-up questions

  • What is alternative percent notation for double-quoted strings?
  • What percent notation matches single quotes?

More Ruby interview questions

View all →