Stefano Crocco
12/30/2007 9:31:00 AM
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