# Dumb str.sub question

**URL:** <https://rubytalk.org/t/dumb-str-sub-question/9061>\
**Category:** ruby-talk\
**Created:** [26 November 2003 18:08 UTC](https://rubytalk.org/t/dumb-str-sub-question/9061 "2003-11-26T18:08:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orion\_Hunter](https://avatars.discourse-cdn.com/v4/letter/o/f1d935/32.png) [@Orion\_Hunter](https://rubytalk.org/u/Orion_Hunter)\
**Post date:** [26 November 2003 18:08 UTC](https://rubytalk.org/t/dumb-str-sub-question/9061/1 "2003-11-26T18:08:17Z")

</div>

I feel really dumb asking this, but I just can’t seem to figure it out.

I am trying to parse some text for use in a mysql insert statement. That  
being the case, I need to escape out quote characters.

If str= “isn’t stands for is not”, what I want is a string with “isn’t  
stands for is not”

At first I thought, sure, no problem!! But I can’t get it. I’ve tried the  
following:

irb:1\> str = "isn’t stands for is not"  
irb:2\> str.sub( ‘’’, ‘’’ ) --\> “isn’t stands for is not” (expected)  
irb:3\> str.sub( ‘’’, ‘\’’ ) --\> unterminated string (expected)  
irb:4\> str.sub( ‘’’, ‘\’ ) --\> “isnt stands for is nott stands for is  
not” (huh? Why this wierd double take?)  
irb:5\> str.sub( ‘’’ ){ |m| m = “’” } --\> “isn’t stands for is not”  
(expected)  
irb:6\> str.sub( ‘’’ ){ |m| m = “\’” } --\> “isn\'t stands for is not” (why  
did it insert TWO 's?, and not just one? I would have exected the first one  
to “escape” the second, thus giving ’ as desired)

So, my quesitons are:

1. How do I do my escape character substituion
2. Why did #4 and #6 behave as they did?

Thanks.

> **···**
>
> * * *
> 
> Gift-shop online from the comfort of home at MSN Shopping! No crowds, free  
> parking. [http://shopping.msn.com](http://shopping.msn.com)

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [26 November 2003 18:47 UTC](https://rubytalk.org/t/dumb-str-sub-question/9061/2 "2003-11-26T18:47:12Z")

</div>

> irb:1\> str = “isn’t stands for is not”  
> irb:4\> str.sub( ‘'’, '\' ) → “isnt stands for is nott stands for is  
> not” (huh? Why this wierd double take?)

Because of the extra processing that substution strings undergo. Within  
the substitution, & is replaced by the portion of the original string  
which matched; ` by the portion before the match; and ' by the portion  
after. So in your case you have this:

```
SEQUENCE	REPLACED BY
\` isn
\& '
\' t stands for is not

```

> irb:6\> str.sub( ‘'’ ){ |m| m = “\'” } → “isn\'t stands for is not”  
> (why did it insert TWO \s?, and not just one? I would have exected the  
> first one to “escape” the second, thus giving ' as desired)

It DIDN’T insert two backslashes; it inserted one, which shows up in  
the inspection of a string as two becuase the inspection uses the  
double-quote syntax. If you print the string out with puts or otherwise  
look into it, you’ll see that there is only one backslash:

```
irb(main):007:0> x = str.sub( '\'' ){ |m| m = "\\'" }
=> "isn\\'t stands for is not"
irb(main):008:0> puts x
isn\'t stands for is not
=> nil
irb(main):009:0> x[2,1]
=> "n"
irb(main):010:0> x[3,1]
=> "\\"
irb(main):011:0> x[4,1]
=> "'"

```

By the way, you can simplify to just this:

```
irb(main):006:0> str.sub(/'/) { '\\\'' }
=> "isn\\'t stands for is not"

```

And you can also use the string form if you use the  
proper number of backslashes:

```
irb(main):002:0> str.sub(/'/, '\\\\\'')
=> "isn\\'t stands for is not"

```

-Mark

> **···**
>
> On Thu, Nov 27, 2003 at 03:08:17AM +0900, Orion Hunter wrote:

---

<div class="post-metadata">

**Author:** ![Austin\_Ziegler2](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@Austin\_Ziegler2](https://rubytalk.org/u/Austin_Ziegler2)\
**Post date:** [26 November 2003 18:50 UTC](https://rubytalk.org/t/dumb-str-sub-question/9061/3 "2003-11-26T18:50:56Z")

</div>

str = "isn’t stands for is not"  
puts str.sub(/’/) { “\’” } #=\> isn’t stands for is not

After execution, irb prints an #inspect on the resulting object.

-austin

> **···**
>
> –  
> austin ziegler \* [austin@halostatue.ca](mailto:austin@halostatue.ca) \* Toronto, ON, Canada  
> software designer \* pragmatic programmer \* 2003.11.26  
> \* 13.48.12

---

<div class="post-metadata">

**Author:** ![Mike\_Stok](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Mike\_Stok](https://rubytalk.org/u/Mike_Stok)\
**Post date:** [26 November 2003 19:22 UTC](https://rubytalk.org/t/dumb-str-sub-question/9061/4 "2003-11-26T19:22:22Z")

</div>

In article [20031126184634.GB1520@mulan.thereeds.org](mailto:20031126184634.GB1520@mulan.thereeds.org),

> **···**
>
> Mark J. Reed [markjreed@mail.com](mailto:markjreed@mail.com) wrote:
> 
> > irb(main):006:0\> str.sub(/'/) { ‘\'’ }  
> > =\> “isn\'t stands for is not”
> 
> Sometimes it is useful to pick your quotes carefully e.g.
> 
> str.sub(/'/) { ‘\'’ }
> 
> as you’re quoting a single quote then using double quotes saves a \
> 
> str.sub(/‘/) { "\’" }
> 
> Mike
> 
> –  
> [mike@stok.co.uk](mailto:mike@stok.co.uk) | The “`Stok’ disclaimers” apply.  
> [http://www.stok.co.uk/~mike/](http://www.stok.co.uk/~mike/) | GPG PGP Key 1024D/059913DA  
> [mike@exegenix.com](mailto:mike@exegenix.com) | Fingerprint 0570 71CD 6790 7C28 3D60  
> [http://www.exegenix.com/](http://www.exegenix.com/) | 75D2 9EC4 C1C0 0599 13DA

---

<div class="post-metadata">

**Author:** ![Mark\_J\_Reed](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@Mark\_J\_Reed](https://rubytalk.org/u/Mark_J_Reed)\
**Post date:** [26 November 2003 19:47 UTC](https://rubytalk.org/t/dumb-str-sub-question/9061/5 "2003-11-26T19:47:11Z")

</div>

Good point. I generally make a habit of using single quotes when  
I’m not doing any interpolation and double-quotes when I am, but this  
would be a logical place to make an exception. 🙂

-Mark

> **···**
>
> On Wed, Nov 26, 2003 at 07:17:55PM +0000, Mike Stok wrote:
> 
> > Sometimes it is useful to pick your quotes carefully e.g.
> > 
> > str.sub(/'/) { ‘\'’ }
> > 
> > as you’re quoting a single quote then using double quotes saves a \
> > 
> > str.sub(/‘/) { "\’" }
