# Getting error as "Can't set variable $\`"

**URL:** <https://rubytalk.org/t/getting-error-as-cant-set-variable/69128>\
**Category:** ruby-talk\
**Created:** [11 February 2014 05:03 UTC](https://rubytalk.org/t/getting-error-as-cant-set-variable/69128 "2014-02-11T05:03:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 February 2014 05:03 UTC](https://rubytalk.org/t/getting-error-as-cant-set-variable/69128/1 "2014-02-11T05:03:59Z")

</div>

Hi, First see the code :

a = 'foo bar baz 12 34 ret'  
b = '2.bar foo tree ream baz foo'

match = Regexp.union(a.split(" "))  
b.gsub(match) { |m| p m }  
# \>\> "bar"  
# \>\> "foo"  
# \>\> "baz"  
# \>\> "foo"

What I am looking for is, for every match inside the block I want to  
substitute the $` to set to empty string(`""`). so that at the end of  
gsub I get at last final string "bar foo baz".

But I am getting error as

a = 'foo bar baz 12 34 ret'  
b = '2.bar foo tree ream baz foo'

match = Regexp.union(a.split(" "))

b.gsub(match) { |m| $`=""; m }  
# ~\> -:6: Can't set variable $`

With gsub how can I do that?

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

---

<div class="post-metadata">

**Author:** ![Mike\_Stok1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/mike_stok1/32/2065_2.png) [@Mike\_Stok1](https://rubytalk.org/u/Mike_Stok1)\
**Post date:** [11 February 2014 13:00 UTC](https://rubytalk.org/t/getting-error-as-cant-set-variable/69128/2 "2014-02-11T13:00:07Z")

</div>

> Hi, First see the code :
> 
> a = 'foo bar baz 12 34 ret'  
> b = '2.bar foo tree ream baz foo'
> 
> match = Regexp.union(a.split(" "))  
> b.gsub(match) { |m| p m }  
> # \>\> "bar"  
> # \>\> "foo"  
> # \>\> "baz"  
> # \>\> "foo"
> 
> What I am looking for is, for every match inside the block I want to  
> substitute the $` to set to empty string(`""`). so that at the end of  
> gsub I get at last final string "bar foo baz".

In the example above you have 4 matches, and you want the final string to be "bar foo baz".

Where do the spaces in the final string come from?

Where did the second "foo" go?

> But I am getting error as
> 
> a = 'foo bar baz 12 34 ret'  
> b = '2.bar foo tree ream baz foo'
> 
> match = Regexp.union(a.split(" "))
> 
> b.gsub(match) { |m| $`=""; m }  
> # ~\> -:6: Can't set variable $`
> 
> With gsub how can I do that?

I dont understand what you are trying to do. If you want to "ignore" the stuff which currently ends up in $` you need to match it as gsub replaces what was matched. For example:

~ ∙ pry  
[1] pry(main)\> a = 'foo bar baz 12 34 ret'  
=\> "foo bar baz 12 34 ret"  
[2] pry(main)\> token\_re = Regexp.union(a.split(" "))  
=\> /foo|bar|baz|12|34|ret/  
[3] pry(main)\> b = '2.bar foo tree ream baz foo'  
=\> "2.bar foo tree ream baz foo"  
[4] pry(main)\> b.gsub(/.\*?(#{token\_re})/, '\1')  
=\> "barfoobazfoo"

might give you a good starting point, I recommend using rubular.com to play with regular expressions and reading [Class: Regexp (Ruby 2.1.0)](http://ruby-doc.org/core-2.1.0/Regexp.html#class-Regexp-label-Repetition) to understand what .\*? does.

If you just want a list of tokens matched then I wouldn't use gsub:

[5] pry(main)\> b.scan(token\_re).join(' ')  
=\> "bar foo baz foo"

and if you just want unique tokens matched:

[8] pry(main)\> b.scan(token\_re).uniq.join(' ')  
=\> "bar foo baz"

Hope this helps,

Mike

> **···**
>
> On Feb 11, 2014, at 12:03 AM, Arup Rakshit \<lists@ruby-forum.com\> wrote:
> 
> --
> 
> Mike Stok \<mike@stok.ca\>  
> [http://www.stok.ca/~mike/](http://www.stok.ca/~mike/)
> 
> The "`Stok' disclaimers" apply.

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [11 February 2014 14:30 UTC](https://rubytalk.org/t/getting-error-as-cant-set-variable/69128/3 "2014-02-11T14:30:56Z")

</div>

Mike Stok wrote in post #1136321:

> Where did the second "foo" go?

If there is a duplicate match, I am interested to take only the first  
one. So last "foo" is not desired.

> > With gsub how can I do that?
> 
> I dont understand what you are trying to do. If you want to "ignore" the  
> stuff which currently ends up in $` you need to match it as gsub  
> replaces what was matched. For example:
> 
> ~ ∙ pry  
> [1] pry(main)\> a = 'foo bar baz 12 34 ret'  
> =\> "foo bar baz 12 34 ret"  
> [2] pry(main)\> token\_re = Regexp.union(a.split(" "))  
> =\> /foo|bar|baz|12|34|ret/  
> [3] pry(main)\> b = '2.bar foo tree ream baz foo'  
> =\> "2.bar foo tree ream baz foo"  
> [4] pry(main)\> b.gsub(/.\*?(#{token\_re})/, '\1')  
> =\> "barfoobazfoo"

This makes sense. Just need to remove the last "foo" that's a challenge.  
Also I can put space with some change in the `'\1'` to `' \1'`.

> If you just want a list of tokens matched then I wouldn't use gsub:
> 
> [5] pry(main)\> b.scan(token\_re).join(' ')  
> =\> "bar foo baz foo"
> 
> and if you just want unique tokens matched:
> 
> [8] pry(main)\> b.scan(token\_re).uniq.join(' ')  
> =\> "bar foo baz"

I know, but this causes performance issue, if you do benchmark, then you  
can see. My mistake I didn't show you that attempt from my side. I wrote  
it first, then tried to thinking using `gsub`.

> **···**
>
> > On Feb 11, 2014, at 12:03 AM, Arup Rakshit \<lists@ruby-forum.com\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
