Quoting strings in Ruby

Topics: Ruby

The bytecode example compares Ruby’s double- and single-quoted strings to address a performance concern.

Double quotes

Some people are concerned that using double quotes by default has a negative impact on performance because of their interpolation behaviour. That’s not the case though, as the byte code analysis shows:

z = RubyVM::InstructionSequence.new <<-eoruby
x = "foobar"
y = 'bazbang'
eoruby
puts z.disasm

Result:

0000 putstring             "foobar"    (   1)[Li]
0002 setlocal_WC_0         x@0
0004 putstring             "bazbang"   (   2)[Li]
0006 dup
0007 setlocal_WC_0         y@1
0009 leave