sub is odd with #

Discussion in 'Ruby' started by Pavel Pvl, Dec 30, 2007.

  1. Pavel Pvl

    Pavel Pvl Guest

    seems to me as though I'm redundant on this forum...


    well anyway, doing '@@@@'.sub('@','#')

    returns
    '/#@@@'

    how can I make it so that it doesn't through in the / ?

    using 1.9 btw

    gsub doesn't do that, but I specifically ned sub to do that.

    ty
    --
    Posted via http://www.ruby-forum.com/.
    Pavel Pvl, Dec 30, 2007
    #1
    1. Advertising

  2. Alle domenica 30 dicembre 2007, Pavel Pvl ha scritto:
    > seems to me as though I'm redundant on this forum...
    >
    >
    > well anyway, doing '@@@@'.sub('@','#')
    >
    > returns
    > '/#@@@'
    >
    > how can I make it so that it doesn't through in the / ?
    >
    > using 1.9 btw
    >
    > gsub doesn't do that, but I specifically ned sub to do that.
    >
    > ty


    First of all, I think what you see is not a '/' but a '\'.

    I don't think the \ is part of the returned string. I assume you're trying
    this in irb. irb displays the return value of every expression using its
    inspect method. String#inspect escapes some characters, such as double quotes
    and the # character, when it would be interpreted as the beginning of string
    interpolation. Since "#@" is the beginning of a string interpolation, it gets
    escaped. If you do a puts '@@@@'.sub('@','#'), or examine the first element
    of the returned string, you'll see that the returned string is correct.

    Using gsub instead of sub doesn't display the \ because it replaces all the @
    with #, and since the sequence '##' isn't a special sequence, it's not
    escaped by String#inspect.

    I hope this helps

    Stefano
    Stefano Crocco, Dec 30, 2007
    #2
    1. Advertising

  3. Pavel Pvl

    Todd Benson Guest

    On Dec 30, 2007 3:11 AM, Pavel Pvl <> wrote:
    > seems to me as though I'm redundant on this forum...
    >
    >
    > well anyway, doing '@@@@'.sub('@','#')
    >
    > returns
    > '/#@@@'


    It should return "\#@@@" (backslash before a character inside a double
    quoted string is an escaped character). You are looking at the
    general string representation of '#@@@'. Do this to see what I mean
    ...

    irb> '@@@@'.sub('@', '#').each_byte { |b| puts b }

    When you use 1.8 you see 4 integers, the first one the byte code for
    the # sign, the others for the @ sign. I think 1.9 is the same way
    with #each_byte, but haven't tried it out.

    >
    > how can I make it so that it doesn't through in the / ?


    irb> puts '@@@@'.sub('@', '#')

    >
    > using 1.9 btw
    >
    > gsub doesn't do that, but I specifically ned sub to do that.


    '####' is not special and doesn't need to be escaped.

    The character sequence '#@' is because it is a short hand way of
    "exploding" a class variable inside a String.

    >
    > ty
    > --
    > Posted via http://www.ruby-forum.com/.


    hth,

    Todd
    Todd Benson, Dec 30, 2007
    #3
  4. Pavel Pvl

    Todd Benson Guest

    On Dec 30, 2007 4:10 AM, Todd Benson <> wrote:
    > On Dec 30, 2007 3:11 AM, Pavel Pvl <> wrote:
    > > seems to me as though I'm redundant on this forum...
    > >
    > >
    > > well anyway, doing '@@@@'.sub('@','#')
    > >
    > > returns
    > > '/#@@@'

    >
    > It should return "\#@@@" (backslash before a character inside a double
    > quoted string is an escaped character). You are looking at the
    > general string representation of '#@@@'. Do this to see what I mean
    > ...
    >
    > irb> '@@@@'.sub('@', '#').each_byte { |b| puts b }
    >
    > When you use 1.8 you see 4 integers, the first one the byte code for
    > the # sign, the others for the @ sign. I think 1.9 is the same way
    > with #each_byte, but haven't tried it out.
    >
    > >
    > > how can I make it so that it doesn't through in the / ?

    >
    > irb> puts '@@@@'.sub('@', '#')
    >
    > >
    > > using 1.9 btw
    > >
    > > gsub doesn't do that, but I specifically ned sub to do that.

    >
    > '####' is not special and doesn't need to be escaped.
    >
    > The character sequence '#@' is because it is a short hand way of
    > "exploding" a class variable inside a String.


    That would be "class instance variable". My bad.

    Todd
    Todd Benson, Dec 30, 2007
    #4
  5. Todd Benson wrote:
    > > The character sequence '#@' is because it is a short hand way of
    > > "exploding" a class variable inside a String.

    >
    > That would be "class instance variable". =A0My bad.


    Well actually it would just be instance variable.


    =2D-=20
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Dec 30, 2007
    #5
  6. Pavel Pvl

    Todd Benson Guest

    On Dec 30, 2007 5:20 AM, Sebastian Hungerecker <> wrote:
    > Todd Benson wrote:
    > > > The character sequence '#@' is because it is a short hand way of
    > > > "exploding" a class variable inside a String.

    > >
    > > That would be "class instance variable". My bad.

    >
    > Well actually it would just be instance variable.


    Newbies should know that that would be the correct phrase, I admit. I
    don't see, however the difference in terminology per se. I said a
    "class instance variable", which would be a variable in the context of
    an instance of a class (class not capitalized).

    > Jabber:
    > ICQ: 205544826


    Todd
    Todd Benson, Dec 30, 2007
    #6
  7. Todd Benson wrote:
    > I
    > don't see, however the difference in terminology per se. =A0I said a
    > "class instance variable", which would be a variable in the context of
    > an instance of a class (class not capitalized).


    I think it gets really complicated once you start to use "class instance=20
    variable" and "Class instance variable", both meaning different things.=20
    Especially at the beginning of a sentence where both would look the same.
    Given that "class instance variable" the way you use it is the same as just=
    =20
    instance variable, I really think it's a lot less confusing to just say the=
    =20
    latter.


    =2D-=20
    Jabber:
    ICQ: 205544826
    Sebastian Hungerecker, Dec 30, 2007
    #7
  8. Pavel Pvl

    Pavel Pvl Guest

    > First of all, I think what you see is not a '/' but a '\'.
    >
    > I don't think the \ is part of the returned string. I assume you're
    > trying
    > this in irb. irb displays the return value of every expression using its
    > inspect method. String#inspect escapes some characters, such as double
    > quotes
    > and the # character, when it would be interpreted as the beginning of
    > string
    > interpolation. Since "#@" is the beginning of a string interpolation, it
    > gets
    > escaped. If you do a puts '@@@@'.sub('@','#'), or examine the first
    > element
    > of the returned string, you'll see that the returned string is correct.

    d by String#inspect.

    > Stefano


    thanks for all the replies! but this was true, i tried it with puts and
    it came out normal. TY!!!
    --
    Posted via http://www.ruby-forum.com/.
    Pavel Pvl, Dec 30, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. THY
    Replies:
    1
    Views:
    385
    Steve C. Orr, MCSD
    Aug 19, 2003
  2. Kathy Burke
    Replies:
    3
    Views:
    382
    Kathy Burke
    Nov 24, 2003
  3. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,050
    Richard Heathfield
    Feb 18, 2007
  4. Ben
    Replies:
    2
    Views:
    853
  5. Lawrence D'Oliveiro

    Death To Sub-Sub-Sub-Directories!

    Lawrence D'Oliveiro, May 5, 2011, in forum: Java
    Replies:
    92
    Views:
    1,940
    Lawrence D'Oliveiro
    May 20, 2011
Loading...

Share This Page