What does %q do in ruby?
%q{abc} is equivalent to writing “abc”. It’s most useful when you have
a string literal that contains a lot of characters you’d need to
escape. Compare:
“Gaba L. asked the ruby-talk list, "What does %q do?"”
vs.
%q{Gaba L. asked the ruby-talk list, “What does %q do?”}
And note they’re equal:
a = %q{Gaba L. asked the ruby-talk list, “What does %q do?”}
b = “Gaba L. asked the ruby-talk list, "What does %q do?"”
a == b
=> true
~ jf
John F.
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack Overflow
Whoops, small typo:
%q{abc} is equivalent to writing “abc”.
should read:
%q{abc} is equivalent to writing ‘abc’ (single quotes).
By contrast, %Q{abc} is equivalent to writing “abc” (double quotes).
You can see the difference when you try to substitute variables:
f = “apple”
%q{#{f}}
=> “#{f}”
%Q{#{f}}
=> “apple”
~ jf
John F.
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack Overflow
Also, lowercase q is like single quotes, but uppercase Q is like double
quotes:
%q(1#{1+1}3) # => “1#{1+1}3”
%Q(1#{1+1}3) # => “123”