How do I get a literal string like /usr\/lib,test\/lib into a variable?
Where are these extra '\' artifacts coming from?
It's just the representation. IRB always shows strings double quoted,
and, in double quotes, a lone \ has to be escaped.
A small example is perhaps more convincing :
22:10 fred@balvenie:~% irb
x = '\/usr\/lib'
=> "\\/usr\\/lib"
x.length
=> 10
'\/usr\/lib' == "\\/usr\\/lib"
=> true
HTH,
Fred
···
Le 5 février 2009 à 21:50, James Byrne a écrit :
--
Any time tomorrow I will try to do what's right
Making sense of all I can
Any time tomorrow I'll pretend to see the light
I just might Shadowman (K's Choice, Shadow Man)
--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!
\ is an escape character for strings. For example, using
a = 'Today is the day, I'm going to New Jersey'
is interpreted as
a = 'Today is the day, I' m going to New Jersey '
This tosses an error because the interpreter assumes the apostrophe in I'm is the end of the string. The \ escapes that apostrophe so that it's merely text in the string, not a Ruby operator. The above line would also error out because 'm', 'going', 'to', 'new', and 'jersey' are unknown methods, and then also because the last ' was read as the beginning of another string, causing the string to run to the end of the file and the interpreter yelling at you because the string wasn't terminated. (In other contexts it would run until the next occurrence of the ' character, tossing other errors.)
irb(main):004:0> p x
"--rails --exclude something_is_not_right \\/usr\\/lib,test\\/lib"
How do I get a literal string like /usr\/lib,test\/lib into a variable?
Where are these extra '\' artifacts coming from?
--
Posted via http://www.ruby-forum.com/\.