G
Gaba Luschi
What does %q do in ruby?
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?\""
=> truea == b
%q{abc} is equivalent to writing "abc".
%q{abc} is equivalent to writing 'abc' (single quotes).
f =3D "apple"
=3D> "apple"%q{#{f}} =3D> "\#{f}"
%Q{#{f}}
%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 =3D %q{Gaba Luschi asked the ruby-talk list, "What does %q do?"}
b =3D "Gaba Luschi asked the ruby-talk list, \"What does %q do?\""=C2=A0=3D> truea =3D=3D b
~ jf
--
John Feminella
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.