# Escaping '

**URL:** <https://www.ruby-forum.com/t/escaping/147480>\
**Category:** Ruby\
**Created:** [September 2, 2008, 10:55am UTC](https://www.ruby-forum.com/t/escaping/147480 "2008-09-02T10:55:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [September 2, 2008, 10:55am UTC](https://www.ruby-forum.com/t/escaping/147480/1 "2008-09-02T10:55:25Z")

</div>

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

Thanks

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [September 2, 2008, 11:39am UTC](https://www.ruby-forum.com/t/escaping/147480/2 "2008-09-02T11:39:14Z")

</div>

David N. 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

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [September 2, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/escaping/147480/3 "2008-09-02T12:27:05Z")

</div>

From: John Pritchard-williams [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# David N. 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

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [September 2, 2008, 1:49pm UTC](https://www.ruby-forum.com/t/escaping/147480/4 "2008-09-02T13:49:21Z")

</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… 🙂
