irb and single quoted strings

Discussion in 'Ruby' started by James Byrne, Feb 5, 2009.

  1. James Byrne

    James Byrne Guest

    This is the problem:

    irb(main):002:0> x = '--rails --exclude something_is_not_right
    \/usr\/lib,test\/lib'
    => "--rails --exclude something_is_not_right \\/usr\\/lib,test\\/lib"

    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?
     
    James Byrne, Feb 5, 2009
    #1
    1. Advertisements

  2. James Byrne

    F. Senault Guest

    Le 5 février 2009 à 21:50, James Byrne a écrit :
    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 [email protected]:~% irb=> true

    HTH,

    Fred
     
    F. Senault, Feb 5, 2009
    #2
    1. Advertisements

  3. James Byrne

    Tim Greer Guest

    You have it right, it's just irb that backwacks it in response.

    irb(main):004:0> x = '--rails -exclude something_is_not_right
    \/usr\/lib,test\/lib'
    => "--rails -exclude something_is_not_right \\/usr\\/lib,test\\/lib"
    irb(main):005:0> puts x
    --rails -exclude something_is_not_right \/usr\/lib,test\/lib
    => nil
    irb(main):006:0>
     
    Tim Greer, Feb 5, 2009
    #3
  4. James Byrne

    Jayce Meade Guest

    \ 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.)

    Same thing happens with the backslash.

    \ = Escape character
    \\ = \ in a string

    Example:

    irb(main):001:0> puts "c:\documents"
    c:documents
    => nil

    Here the \ is merely an escape character and thus does nothing since there's
    no special characters after it. On the other hand:

    irb(main):002:0> puts "c:\\documents"
    c:\documents
    => nil

    The \\ becomes \ because it used the first backslash as the escape character
    for the second backslash.

    I hope that helps.

    - jayce
     
    Jayce Meade, Feb 5, 2009
    #4
    1. Advertisements

Ask a Question

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 (here). After that, you can post your question and our members will help you out.