# Escaping '

**URL:** <https://rubytalk.org/t/escaping/48802>\
**Category:** ruby-talk\
**Created:** [2 September 2008 08:50 UTC](https://rubytalk.org/t/escaping/48802 "2008-09-02T08:50:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/d/b38774/32.png) [@David\_Nguyen](https://rubytalk.org/u/David_Nguyen)\
**Post date:** [2 September 2008 08:50 UTC](https://rubytalk.org/t/escaping/48802/1 "2008-09-02T08:50:41Z")

</div>

Hi,  
I need to replace ' by \'. How can I do that?

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![John\_Pritchard-Willi](https://avatars.discourse-cdn.com/v4/letter/j/d9b06d/32.png) [@John\_Pritchard-Willi](https://rubytalk.org/u/John_Pritchard-Willi)\
**Post date:** [2 September 2008 09:33 UTC](https://rubytalk.org/t/escaping/48802/2 "2008-09-02T09:33:51Z")

</div>

David Nguyen wrote:

> Hi,  
> I need to replace ' by \'. How can I do that?
> 
> Thanks

This was harder than I thought...in "vi"(vim actually) I just had to:

1,$ s/'/\\'/g

But Ruby didn't seem to like that....

Anyway, for reasons unknown to me, this works:

puts "'hello'".gsub("'","\\\\'")

Cheers

John

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [2 September 2008 10:21 UTC](https://rubytalk.org/t/escaping/48802/3 "2008-09-02T10:21:22Z")

</div>

# David Nguyen wrote:  
# \> I need to replace ' by \'. How can I do that?  
# This was harder than I thought...in "vi"(vim actually) I just had to:  
# 1,$ s/'/\\'/g  
# But Ruby didn't seem to like that....  
# Anyway, for reasons unknown to me, this works:

how i wish the behaviour would change.  
search the archives. this is a faq 🙂

# puts "'hello'".gsub("'","\\\\'")

if like me, you want to lessen the those fences 🙂  
try,

"'hello'".gsub("'") { "\\'" }  
#=\> "\\'hello\\'"

or

"'hello'".gsub "'", %q[\\\']  
#=\> "\\'hello\\'"

for obvious reason, my brain prefers the former

kind regards -botp

> **···**
>
> From: John Pritchard-williams [[mailto:monojohnny@googlemail.com](mailto:monojohnny@googlemail.com)]

---

<div class="post-metadata">

**Author:** ![John\_Pritchard-Willi](https://avatars.discourse-cdn.com/v4/letter/j/d9b06d/32.png) [@John\_Pritchard-Willi](https://rubytalk.org/u/John_Pritchard-Willi)\
**Post date:** [2 September 2008 11:43 UTC](https://rubytalk.org/t/escaping/48802/4 "2008-09-02T11:43:57Z")

</div>

//how i wish the behaviour would change.  
search the archives. this is a faq :)//

oops sorry (I did it again)...perhaps you could include the link to  
where this is explained though... 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
