# Gsub and single quote issue

**URL:** <https://www.ruby-forum.com/t/gsub-and-single-quote-issue/163786>\
**Category:** Ruby\
**Created:** [March 24, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/gsub-and-single-quote-issue/163786 "2009-03-24T21:35:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 24, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/gsub-and-single-quote-issue/163786/1 "2009-03-24T21:35:41Z")

</div>

a = " Ruby’s gem "

puts a.gsub(/[’]/, “”) #=\> 1. Rubys gem

puts a.gsub(/[’]/, “’”) #=\> 2. Ruby’s gem

puts a.gsub(/[’]/, “’”) #=\> 3. Ruby’s gem

puts a.gsub(/[’]/, “\’”) #=\> 4. Rubys gem s gem

puts a.gsub(/[’]/, “\’”) #=\> 5. Rubys gem s gem

puts a.gsub(/[’]/, “\\’”) #=\> 6. Ruby’s gem

puts " "

I couldn’t understand the result for #4 and #5. Any explanation is  
appreciated.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 24, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/gsub-and-single-quote-issue/163786/2 "2009-03-24T22:15:30Z")

</div>

On 24.03.2009 21:32, Raj S. wrote:

> puts a.gsub(/[’]/, “\’”) #=\> 5. Rubys gem s gem
> 
> puts a.gsub(/[’]/, “\\’”) #=\> 6. Ruby’s gem
> 
> puts " "
> 
> I couldn’t understand the result for #4 and #5. Any explanation is  
> appreciated.

The sequence ’ in a replacement string is interpreted as the post  
match. That’s why you see the duplication. You’ll see this nicely by  
bracketing the replacement:

irb(main):012:0\> a = " Ruby’s gem "  
=\> " Ruby’s gem "  
irb(main):013:0\> a.gsub /’/, “\<\’\>”  
=\> " Ruby ~~s gem "~~

There is also pre match:

irb(main):014:0\> a.gsub /’/, “\<\`\>”  
=\> " Ruby\< Ruby\>s gem "

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 25, 2009, 6:21am UTC](https://www.ruby-forum.com/t/gsub-and-single-quote-issue/163786/3 "2009-03-25T06:21:49Z")

</div>

Robert K. wrote:

> The sequence ’ in a replacement string is interpreted as the post  
> match.

I assume that has something to do with ruby’s crazy global variables,  
e.g $’. In that case, why isn’t the ‘$’ required.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [March 25, 2009, 8:10am UTC](https://www.ruby-forum.com/t/gsub-and-single-quote-issue/163786/4 "2009-03-25T08:10:44Z")

</div>

On 25.03.2009 06:18, 7stud – wrote:

> Robert K. wrote:
> 
> > The sequence ’ in a replacement string is interpreted as the post  
> > match.
> 
> I assume that has something to do with ruby’s crazy global variables,  
> e.g $’. In that case, why isn’t the ‘$’ required.

The replacement string is interpreted by sub and gsub and this is just  
the convention that was chosen. Btw, it’s the same convention as in sed  
and a number of other regular expression based replacement tools as  
well.

Kind regards

robert
