Ask a Question related to Ruby, Design and Development.
-
Ron Coutts #1
Backslash substitution question
I'm having trouble with backslashes and I don't know what is wrong. I
can't seem to write and expression that will evaluate to a string
containing a single backslash character as in "\". It seems that "\\"
in Ruby evaluates to "\\" not to "\" as I would expect. Below are a few
things I've tried. If anyone could send back a quick answer it would be
much appreciated.
res = "c:/foo/bar".gsub(/\//, "\\") # -> c:\\foo\\bar
res = "c:/foo/bar".gsub(/\//, '\\') # -> c:\\foo\\bar
#res = "c:/foo/bar".gsub(/\//, "\") # -> syntax error - unterminated
string meets end of file
#res = "c:/foo/bar".gsub(/\//, '\') # -> syntax error - unterminated
string meets end of file
res = "c:/foo/bar".gsub(/\//) { "\\"} # -> c:\\foo\\bar
Ron
Ron Coutts Guest
-
#include substitution question
hi, the #include directive is invoked at compile time. i need to use something simular that will be invoked at runtime. this nice thing about... -
Backslash issue
Hi! I would very much like to know how I can get this to work: $filepath = ereg_replace("\\", "\",$filepath); I know backslash in a string tells... -
Backslash substitution question<Pine.LNX.4.44.0311111730390.16432-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Wed, 12 Nov 2003, Ron Coutts wrote: Are you doing this in irb? If so, you'll get a quoted and escaped version of the new string... -
backslash
Hi I created a table with varchar field where i want to store the path of an image. I will then use this file to retrieve this image on my... -
Substitution Question
I simply want to convert Adkins to Adki01 This code is close, but has a space between Adki and 01 $temp =~ s/^(.{0,4})..$/$1 01/; If I take... -
Hal Fulton #2
Re: Backslash substitution question
Ron Coutts wrote:
This one works. You're getting confused by either irb> I'm having trouble with backslashes and I don't know what is wrong. I
> can't seem to write and expression that will evaluate to a string
> containing a single backslash character as in "\". It seems that "\\"
> in Ruby evaluates to "\\" not to "\" as I would expect. Below are a few
> things I've tried. If anyone could send back a quick answer it would be
> much appreciated.
>
> res = "c:/foo/bar".gsub(/\//, "\\") # -> c:\\foo\\bar
or by calling p. You're seeing the escaped form, but it's
not stored that way internally.
Note that res.length is 10, not 12.
Hal
Hal Fulton Guest
-
Eric Hodel #3
Re: Backslash substitution question
--oJAv8lSwuaQsYd0G
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Ron Coutts (rcoutts@envistatech.com) wrote:
Ruby maps between / and \ in filenames on win32 just fine, why not let> I'm having trouble with backslashes and I don't know what is wrong. I
> can't seem to write and expression that will evaluate to a string
> containing a single backslash character as in "\". It seems that "\\"
> in Ruby evaluates to "\\" not to "\" as I would expect. Below are a few
> things I've tried. If anyone could send back a quick answer it would be
> much appreciated.
>=20
> res =3D "c:/foo/bar".gsub(/\//, "\\") # -> c:\\foo\\bar
> res =3D "c:/foo/bar".gsub(/\//, '\\') # -> c:\\foo\\bar
> #res =3D "c:/foo/bar".gsub(/\//, "\") # -> syntax error - unterminated
> string meets end of file
> #res =3D "c:/foo/bar".gsub(/\//, '\') # -> syntax error - unterminated
> string meets end of file
> res =3D "c:/foo/bar".gsub(/\//) { "\\"} # -> c:\\foo\\bar
it do the work for you? For cmd.exe, it doesn't cane if you give it \
or /, but tab completion doesn't work on /. (I believe I read somewhere
that the guts don't care, since back in the DOS days, but my mind could
be addled).
See also File.join and File.expand_path
--=20
Eric Hodel - [email]drbrain@segment7.net[/email] - [url]http://segment7.net[/url]
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04
--oJAv8lSwuaQsYd0G
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (FreeBSD)
iD8DBQE/sWJPMypVHHlsnwQRAop4AKC9nNPPYa/D6DwkAapjPBAf5rB2NQCdFDv9
48mLS3fn2QLE6lMZMghOr88=
=H0Ef
-----END PGP SIGNATURE-----
--oJAv8lSwuaQsYd0G--
Eric Hodel Guest
-
Ryan Pavlik #4
Re: Backslash substitution question
On Wed, 12 Nov 2003 07:21:51 +0900
"Ron Coutts" <rcoutts@envistatech.com> wrote:
In any ruby string literal, backslash escapes the next character.> I'm having trouble with backslashes and I don't know what is wrong. I
> can't seem to write and expression that will evaluate to a string
> containing a single backslash character as in "\". It seems that "\\"
> in Ruby evaluates to "\\" not to "\" as I would expect. Below are a few
> things I've tried. If anyone could send back a quick answer it would be
> much appreciated.
This is so you can write:
puts "\"hello world\"" # => "hello world"
It escapes itself, too, so you have a way of printing backslashes:
puts "\\" # => \
It works inside single quotes, so you can escape single quotes:
puts '\'' # => '
That means it needs to escape itself as well, otherwise you couldn't
print a backslash:
puts '\\' # => \
This also works inside docstrings.
hth,
--
Ryan Pavlik <rpav@mephle.com>
"*More* hapless visitors? Tsk, and I'm all out of doom quests." - 8BT
Ryan Pavlik Guest
-
Ryan Pavlik #5
Re: Backslash substitution question
On Wed, 12 Nov 2003 07:30:08 +0900
Ryan Pavlik <rpav@mephle.com> wrote:
<basic obvious stuff>
Sorry, after seeing others post, I realize I misunderstood your
question. Yeah, what they said. Always test with puts/print. ;-)
--
Ryan Pavlik <rpav@mephle.com>
"*More* hapless visitors? Tsk, and I'm all out of doom quests." - 8BT
Ryan Pavlik Guest
-
Ron Coutts #6
Re: Backslash substitution question
Thanks for all the help everybody and the quick turnaround, you've saved
me some valuable time. I wasn't aware of the 'quoted and escaped'
versions of these strings.
To reply to Eric, usually on win32 I just use '/' and let Ruby handle it
for me but the script I'm working on now has to enter this string into a
Windows GUI app edit field and that app doesn't like '/', that's why I
had to convert it myself.
Hal, what did you mean by 'calling p'?
Thanks again,
Ron
> -----Original Message-----
> From: Hal Fulton [mailto:hal9000@hypermetrics.com]
> Sent: November 11, 2003 3:26 PM
> To: ruby-talk ML
> Subject: Re: Backslash substitution question
>
>
> Ron Coutts wrote:> is wrong. I> > I'm having trouble with backslashes and I don't know what> seems that "\\"> > can't seem to write and expression that will evaluate to a string
> > containing a single backslash character as in "\". It> Below are a few> > in Ruby evaluates to "\\" not to "\" as I would expect.> answer it would be> > things I've tried. If anyone could send back a quick>> > much appreciated.
> >
> > res = "c:/foo/bar".gsub(/\//, "\\") # -> c:\\foo\\bar
> This one works. You're getting confused by either irb
> or by calling p. You're seeing the escaped form, but it's
> not stored that way internally.
>
> Note that res.length is 10, not 12.
>
> Hal
>
>
Ron Coutts Guest
-
Hal Fulton #7
Re: Backslash substitution question
Ron Coutts wrote:
The p method is similar to puts or print, but it invokes> Hal, what did you mean by 'calling p'?
the 'inspect' method on the object, so it's displayed
in a specialized (or user-defined) way.
p "\\" # "\\"
puts "\\" # \
Useful for debugging, especially if you create "pretty"
inspect methods.
Hal
Hal Fulton Guest



Reply With Quote

