What does %q do in ruby?
···
--
Posted via http://www.ruby-forum.com/.
What does %q do in ruby?
--
Posted via http://www.ruby-forum.com/.
%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 Luschi asked the ruby-talk list, \"What does %q do?\""
vs.
%q{Gaba Luschi asked the ruby-talk list, "What does %q do?"}
And note they're equal:
a = %q{Gaba Luschi asked the ruby-talk list, "What does %q do?"}
b = "Gaba Luschi asked the ruby-talk list, \"What does %q do?\""
a == b
=> true
~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack Overflow
On Sun, Feb 13, 2011 at 19:39, Gaba Luschi <friedoysterlover@gmail.com> wrote:
What does %q do in ruby?
--
Posted via http://www.ruby-forum.com/\.
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"
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 Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack Overflow
On Sun, Feb 13, 2011 at 19:46, John Feminella <johnf@bitsbuilder.com> wrote:
%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 Luschi asked the ruby-talk list, \"What does %q do?\""
vs.
%q{Gaba Luschi asked the ruby-talk list, "What does %q do?"}
And note they're equal:
a = %q{Gaba Luschi asked the ruby-talk list, "What does %q do?"}
b = "Gaba Luschi asked the ruby-talk list, \"What does %q do?\""a == b
=> true
~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack OverflowOn Sun, Feb 13, 2011 at 19:39, Gaba Luschi <friedoysterlover@gmail.com> wrote:
What does %q do in ruby?
--
Posted via http://www.ruby-forum.com/\.