Ruby single and double quotes

In Ruby, double quotes are interpolated, meaning the code in #{} is evaluated as Ruby. Single quotes are treated as literals (meaning the code isn’t evaluated).

var = "hello"
"#{var} world" #=> "hello world"
'#{var} world' #=> "#{var} world"

For some extra-special magic, Ruby also offers another way to create strings:

%Q() # behaves like double quotes
%q() # behaves like single quotes

For example:

%Q(#{var} world) #=> "hello world"
%q(#{var} world) #=> "#{var} world"

Leave a Comment