Ruby - What's the Difference Between Single and Double Quotes

Double vs single quotes

" " allows you to do string interpolation, e.g.:

world_type = 'Mars'
"Hello #{world_type}"

Ruby - what's the difference between single and double quotes?

Yes, single-quoted strings don't process ASCII escape codes and they don't do string interpolation.

name = 'Joe'
greeting = 'Hello, #{name}' # this won't produce "Hello, Joe"

what is the difference between single quote ( ' ) and double quote ( ) in cocoapod?

Single quotes are used for just strings.

Where as Double quotes are used for both strings and string interpolation.

Example:

name = 'User Name' //User Name

welcome_note_using_sigle_qoute = 'Welcome #{name}' //Welcome /#{name}
welcome_note_using_double_qoute = "Welcome #{name}" //Welcome User Name

Difference between double quotes and single quotes in Ruby

Double-quotes interpolates.

Single-quotes do not, e.g.,

puts "Hi #{42+5}"
=> "Hi 47"

puts 'Hi #{42+5}'
=> "Hi #{42+5}"

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"

When to use single vs. double quotes in a Rails app

The difference between using double quotes versus single quotes is that double quotes interpret escaped characters and single quotes preserve them.

Here is an example

 puts "i \n love \n ruby"
#i
#love
#ruby

and

 puts 'i \n love \n ruby'
#i \n love \n ruby

What is the difference between using single quotes and double quotes to query a Hash in Ruby?

That's strange, they should work the same way and do for me:

>> user = {"user"=>
.. {"bio"=>"rubyist",
.. "created_at"=>"2011-05-03T15:21:46+02:00",
.. "email"=>"paul@pauldix.net",
.. "id"=>61, "name"=>"paul",
.. "updated_at"=>"2011-05-03T15:21:46+02:00"}}.to_json
#=> "{"user":{"bio":"rubyist","created_at":"2011-05-03T15:21:46+02:00","email":"paul@pauldix.net","id":61,"name":"paul","updated_at":"2011-05-03T15:21:46+02:00"}}"
>> attributes = JSON.parse(user)["user"]
#=> {"bio"=>"rubyist", "created_at"=>"2011-05-03T15:21:46+02:00", "email"=>"paul@pauldix.net", "id"=>61, "name"=>"paul", "updated_at"=>"2011-05-03T15:21:46+02:00"}
>> attributes = JSON.parse(user)['user']
#=> {"bio"=>"rubyist", "created_at"=>"2011-05-03T15:21:46+02:00", "email"=>"paul@pauldix.net", "id"=>61, "name"=>"paul", "updated_at"=>"2011-05-03T15:21:46+02:00"

Is there a performance gain in using single quotes vs double quotes in ruby?

$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.0.0]

$ cat benchmark_quotes.rb
# As of Ruby 1.9 Benchmark must be required
require 'benchmark'

n = 1000000
Benchmark.bm(15) do |x|
x.report("assign single") { n.times do; c = 'a string'; end}
x.report("assign double") { n.times do; c = "a string"; end}
x.report("concat single") { n.times do; 'a string ' + 'b string'; end}
x.report("concat double") { n.times do; "a string " + "b string"; end}
end

$ ruby benchmark_quotes.rb

user system total real
assign single 0.110000 0.000000 0.110000 ( 0.116867)
assign double 0.120000 0.000000 0.120000 ( 0.116761)
concat single 0.280000 0.000000 0.280000 ( 0.276964)
concat double 0.270000 0.000000 0.270000 ( 0.278146)

Note: I've updated this to make it work with newer Ruby versions, and cleaned up the header, and run the benchmark on a faster system.

This answer omits some key points. See especially these other answers concerning interpolation and the reason there is no significant difference in performance when using single vs. double quotes.



Related Topics



Leave a reply



Submit