Professional Web Applications Themes

Backslash substitution question - Ruby

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 = ...

  1. #1

    Default 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

  2. #2

    Default Re: Backslash substitution question

    Ron Coutts wrote:
    > 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
    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


    Hal Fulton Guest

  3. #3

    Default Re: Backslash substitution question

    --oJAv8lSwuaQsYd0G
    Content-Type: text/plain; cht=us-ascii
    Content-Disposition: inline
    Content-Transfer-Encoding: quoted-printable

    Ron Coutts (rcouttsenvistatech.com) wrote:
    > 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
    Ruby maps between / and \ in filenames on win32 just fine, why not let
    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]drbrainsegment7.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

  4. #4

    Default Re: Backslash substitution question

    On Wed, 12 Nov 2003 07:21:51 +0900
    "Ron Coutts" <rcouttsenvistatech.com> wrote:
    > 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.
    In any ruby string literal, backslash escapes the next character.
    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 <rpavmephle.com>

    "*More* hapless visitors? Tsk, and I'm all out of doom quests." - 8BT

    Ryan Pavlik Guest

  5. #5

    Default Re: Backslash substitution question

    On Wed, 12 Nov 2003 07:30:08 +0900
    Ryan Pavlik <rpavmephle.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 <rpavmephle.com>

    "*More* hapless visitors? Tsk, and I'm all out of doom quests." - 8BT

    Ryan Pavlik Guest

  6. #6

    Default 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:hal9000hypermetrics.com]
    > Sent: November 11, 2003 3:26 PM
    > To: ruby-talk ML
    > Subject: Re: Backslash substitution question
    >
    >
    > Ron Coutts wrote:
    > > 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
    >
    > 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

  7. #7

    Default Re: Backslash substitution question

    Ron Coutts wrote:
    > Hal, what did you mean by 'calling p'?
    The p method is similar to puts or print, but it invokes
    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

Similar Threads

  1. #include substitution question
    By difoggio in forum Macromedia Flash Data Integration
    Replies: 1
    Last Post: March 5th, 04:33 AM
  2. Replies: 0
    Last Post: November 11th, 10:29 PM
  3. Replies: 1
    Last Post: September 17th, 02:56 PM
  4. backslash
    By janet in forum ASP
    Replies: 1
    Last Post: August 22nd, 09:56 AM
  5. Substitution Question
    By Mike Flannigan in forum PERL Miscellaneous
    Replies: 8
    Last Post: August 2nd, 05:42 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139