gsub(/\s*$/, "") doubling string

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default gsub(/\s*$/, "") doubling string

    Hello,
    I recently downloaded ruby 1.8.0 p3, (2003-06-23) [i686-linux], and
    tried it on some code that chopped trailing spaces from a string using
    gsub(/\s*$/, ""). I'm seeing some odd behavior and was hoping someone
    could shed some light on what's happening.

    In 1.6.7 the code above did what I expected. However, in 1.8.0p3 it
    seems to double the string if the string doesn't end in a space.

    #does what I expected in 1.6 and 1.8
    irb(main):004:0> " TEST ".gsub(/\s*$/, "")
    => " TEST"

    # very odd (at least to me) in 1.8.0p3
    irb(main):005:0> " TEST .".gsub(/\s*$/, "")
    => " TEST . TEST ."

    Where does this repeat come from? If I change the * to a + it fixes my
    problem but I was hoping someone could help explain why it's happening.

    While looking into this I've noticed that there seems to be something
    special about 2 repeats.

    irb(main):002:0> " string ".gsub(/\s*$/, 'P')
    " stringPP"

    Regardless of how many trailing spaces I add two Ps are always
    appended. It seems that one matches all the spaces and then one matches
    the zero length string that's the end itself since a string without
    trailing spaces puts in one P. Is the $ getting used twice in this
    match?

    I'd appreciate any explanations or help.
    thank you,
    Paul

    Paul Rubel Guest

  2. Similar Questions and Discussions

    1. "Error Creating Control" and "Cast from String"
      I'm creating a custom date control. In appearance, it's just a textbox and a button. It has three custom properties: CalDate, CalDateType and...
    2. #26292 [Opn->Bgs]: substr returns "0" for any offset on the string "0"
      ID: 26292 Updated by: sniper@php.net Reported By: ravacholp at hotmail dot com -Status: Open +Status: ...
    3. #25763 [Opn->WFx]: Why the string "1.10" is equal to the string "1.1"?
      ID: 25763 Updated by: pollita@php.net Reported By: jparneodo at yahoo dot fr -Status: Open +Status: ...
    4. #25763 [NEW]: Why the string "1.10" is equal to the string "1.1"?
      From: jparneodo at yahoo dot fr Operating system: RH7.2 PHP version: 4.3.3 PHP Bug Type: Strings related Bug description: ...
    5. convert visual basic "string" data type to DB2 "blob"
      Does anyone know if a visual basic string data type can be converted to DB2 blob datatype? I have all data in XML files and I use Visual Basic to...
  3. #2

    Default Re: gsub(/\s*$/, "") doubling string

    I don't know what the problem is, but you should probably be using
    gsub(/\s+$/,'') anyway :o)

    -Kurt

    On Tue, Jul 22, 2003 at 03:36:56AM +0900, Paul Rubel wrote:
    > Hello,
    > I recently downloaded ruby 1.8.0 p3, (2003-06-23) [i686-linux], and
    > tried it on some code that chopped trailing spaces from a string using
    > gsub(/\s*$/, ""). I'm seeing some odd behavior and was hoping someone
    > could shed some light on what's happening.
    >
    > In 1.6.7 the code above did what I expected. However, in 1.8.0p3 it
    > seems to double the string if the string doesn't end in a space.
    >
    > #does what I expected in 1.6 and 1.8
    > irb(main):004:0> " TEST ".gsub(/\s*$/, "")
    > => " TEST"
    >
    > # very odd (at least to me) in 1.8.0p3
    > irb(main):005:0> " TEST .".gsub(/\s*$/, "")
    > => " TEST . TEST ."
    >
    > Where does this repeat come from? If I change the * to a + it fixes my
    > problem but I was hoping someone could help explain why it's happening.
    >
    > While looking into this I've noticed that there seems to be something
    > special about 2 repeats.
    >
    > irb(main):002:0> " string ".gsub(/\s*$/, 'P')
    > " stringPP"
    >
    > Regardless of how many trailing spaces I add two Ps are always
    > appended. It seems that one matches all the spaces and then one matches
    > the zero length string that's the end itself since a string without
    > trailing spaces puts in one P. Is the $ getting used twice in this
    > match?
    >
    > I'd appreciate any explanations or help.
    > thank you,
    > Paul
    >
    >======= End of Original Message =======<
    Kurt M. Dresner Guest

  4. #3

    Default Re: gsub(/\s*$/, "") doubling string

    Hi,

    In message "Re: gsub(/\s*$/, "") doubling string"
    on 03/07/22, ts <decoux@moulon.inra.fr> writes:

    |P> # very odd (at least to me) in 1.8.0p3
    |P> irb(main):005:0> " TEST .".gsub(/\s*$/, "")
    |P> => " TEST . TEST ."
    |
    | Can you try this

    Thank you for the fix, Guy. And thank you for finding a bug, Paul.

    |P> irb(main):002:0> " string ".gsub(/\s*$/, 'P')
    |P> " stringPP"
    |
    | this is normal

    The first try replaces all trailing spaces with "P", then second try
    replaces empty at the end of string with "P".

    matz.

    Yukihiro Matsumoto Guest

Posting Permissions

  • You may not post new threads
  • You may 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