# Sub is odd with #

**URL:** <https://www.ruby-forum.com/t/sub-is-odd-with/124566>\
**Category:** Ruby\
**Created:** [December 30, 2007, 10:11am UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566 "2007-12-30T10:11:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 30, 2007, 10:11am UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566/1 "2007-12-30T10:11:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 30, 2007, 10:31am UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566/2 "2007-12-30T10:31:37Z")

</div>

Alle domenica 30 dicembre 2007, Pavel P. ha scritto:

> 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

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 30, 2007, 11:10am UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566/3 "2007-12-30T11:10:47Z")

</div>

On Dec 30, 2007 3:11 AM, Pavel P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://www.ruby-forum.com/).

hth,

Todd

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 30, 2007, 11:14am UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566/4 "2007-12-30T11:14:53Z")

</div>

On Dec 30, 2007 4:10 AM, Todd B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> quoted string is an escaped character). You are looking at the
> 
> > how can I make it so that it doesn’t through in the / ?  
> > 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

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 30, 2007, 12:22pm UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566/5 "2007-12-30T12:22:04Z")

</div>

Todd B. 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.

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 30, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566/6 "2007-12-30T16:42:48Z")

</div>

On Dec 30, 2007 5:20 AM, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Todd B. 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: removed\_email\_address@domain.invalid  
> ICQ: 205544826

Todd

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 30, 2007, 4:59pm UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566/7 "2007-12-30T16:59:51Z")

</div>

Todd B. wrote:

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

I think it gets really complicated once you start to use “class instance  
variable” and “Class instance variable”, both meaning different things.  
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  
instance variable, I really think it’s a lot less confusing to just say  
the  
latter.

---

<div class="post-metadata">

**Author:** ![pavel989](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/48db29/32.png) [@pavel989](https://www.ruby-forum.com/u/pavel989)\
**Post date:** [December 30, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/sub-is-odd-with/124566/8 "2007-12-30T18:18:15Z")

</div>

> 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!!!
