anakin
anakin

Reputation: 365

Ruby single and double quotes

I've recently been coding in Ruby and have come from Python, where single and double quotes made no difference to how the code worked as far as I know.

I moved to Ruby to see how it worked, and to investigate the similarities between Ruby and Python.

I was using single-quoted strings once and noticed this:

hello = 'hello'
x = '#{hello} world!'
puts x

It returned '#{hello} world!' rather than 'hello world!'.

After noticing this I tried double quotes and the problem was fixed. Now I'm not sure why that is.

Do single and double quotes change this or is it because of my editor (Sublime text 3)? I'm also using Ruby version 2.0 if it works differently in previous versions.

Upvotes: 8

Views: 4450

Answers (6)

I am PK
I am PK

Reputation: 6774

In the string interpolation concept, the essential difference between using single or double quotes is that double quotes allow for escape sequences while single quotes do not.

Let's take an example:

name = "Mike"
puts "Hello #{name} \n How are you?"

The above ruby code with string interpolation will interpolate the variable called name which is written inside brackets with its original value which is Mike. And it will also print the string How are you? in a separate line since we already placed an escape sequence there.

Output:

Hello Mike 
  How are you?

If you do the same with single quotes, it will treat the entire string as a text and it will print as it is including the escape sequence as well.

name = Mike'
puts 'Hello #{name} \n How are you'?

Output:

Hello #{name} \n How are you?

Upvotes: 0

Jaimin pandya
Jaimin pandya

Reputation: 493

Ruby supports single-quoted string, for many uses like as follow:

>> 'foo'
=> "foo"
>> 'foo' + 'bar'
=> "foobar"

In above example, those two types of strings are identical. We can use double quote in place of single quote and we will get same output like above example.

As you face problem, while using interpolation in single quoted string because Ruby do not interpolate into single-quoted string. I am taking one example for more understanding:

>> '#{foo} bar'
=> "\#{foo} bar"

Here you can see that return values using double-quoted strings, which requires backslash to escape special characters such as #.

Single quoted string often useful because they are truly literal.

Upvotes: 1

Borodin
Borodin

Reputation: 126772

You should read the Literals section of the official Ruby documentation.

It is very concise, so you need to read carefully. But it explains the difference between double-quoted and single-quoted strings, and how they are equivalent to %Q/.../ and %q/.../ respectively.

Upvotes: 3

hwnd
hwnd

Reputation: 70750

Single-quoted strings don't process escape sequence \ and they don't do string interpolation. For a better understanding, take a look at String concatenation vs. interpolation

To answer your question, you have to use "" when you want to do string interpolation:

name = 'world'
puts "Hello #{name}" # => "Hello world"

Using escape sequence:

puts 'Hello\nworld'       # => "Hello\nworld"
puts "Hello\nworld"       # => "Hello
                                world"

Upvotes: 2

sethvargo
sethvargo

Reputation: 26997

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"

Upvotes: 11

Marek Lipka
Marek Lipka

Reputation: 51191

If you enclose Ruby string in single qoutes, you can't use interpolation. That's how Ruby works.

Upvotes: 2

Related Questions