Ruby : Difference between single/double quotation

Not so with double quoted strings. Double quoted strings are pre-processed in some familiar ways:

    • \n becomes a single linefeed (also known as a newline) character,
      • So while the string ‘abc\n’ is five characters long (the last two characters being a backslash and the letter “n”), the string “abc\n” is only four characters long (the last character being a linefeed character).
      • puts 'a\nb' # just print a\nb
        puts "a\nb" # print a, then b at newline
        puts "\1" -> get special character
        puts '\1' -> get \1
    • while \t becomes a tab.
    • Finally, you may have noticed that no \n appeared at the end of the ‘hello world’ string that we fed to puts. Nevertheless, puts printed our message with a newline. It turns out that puts is actually fairly clever and will stick a newline character on the end of any output that lacks one. This behavior is not necessarily desirable for precision formatting. (Don’t get Confused)
2.1.1 :001 > puts 'hello world!'
hello world!
 => nil
2.1.1 :002 > puts "hello world!"
hello world!
 => nil
2.1.1 :003 > puts "hello world!"; puts "this is Shiva"
hello world!
this is Shiva
 => nil
2.1.1 :004 > puts 'hello world!'; puts 'this is Shiva, again'
hello world!
this is Shiva, again
 => nil
2.1.1 :005 >


I too thought that single quoted strings might be quicker to parse for Ruby. It doesn’t seem to be the case.

Anyway, I think the above benchmark are measuring the wrong thing, though. It stands to reason that either versions will be parsed into the same internal string representations so to get the answer as to which is quicker to parse, we shouldn’t be measuring performance with string variables, but rather Ruby’s speed of parsing strings.

10000.times do
  ('a'..'z').to_a.each {|v| print "#{v}='This is a test string.'\n" }
#Generate sample ruby code with lots of strings to parse
$ ruby generate.rb > single_q.rb
#Get the double quote version
$ tr \' \" < single_q.rb > double_q.rb
#Compare execution times
$ time ruby single_q.rb
real    0m0.978s
user    0m0.920s
sys     0m0.048s
$ time ruby double_q.rb
real    0m0.994s
user    0m0.940s
sys     0m0.044s

Repeated runs don’t seem to make much difference. It still takes pretty much the same time to parse either version of the string.

References :

Book – Design pattern in Ruby

Stack Question:

One thought on “Ruby : Difference between single/double quotation

  1. Using benchmarks like this are misleading in the way they are presented here. Also using the OS to benchmark vs Ruby benchmarking seems like a huge miss in this article. Other considerations are GC, heap caching, scope, any other possible Ruby optimizations that obfuscate the data in point you are making… Even if the difference does not exist, the article should present that in an empirical manner scoped to the problem and remove other variables.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s