# Help with gsub

**URL:** <https://www.ruby-forum.com/t/help-with-gsub/172214>\
**Category:** Rails\
**Created:** [July 16, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/help-with-gsub/172214 "2009-07-16T20:43:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [July 16, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/help-with-gsub/172214/1 "2009-07-16T20:43:57Z")

</div>

I have a string of state codes (ie ‘MD,PA,VA,WV’) i’m passing from a  
select list (:allow\_multiple =\> true), in which I’m trying to replace  
the commas with “’,’”. I have the following gsub, which works fine in  
irb, however in Rails it’s returning “’’,’’” (double single-quotes).

states = params[:states].gsub(/,/, “’,’”)

Any suggestions?

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [July 16, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/help-with-gsub/172214/2 "2009-07-16T21:17:44Z")

</div>

Brian P. wrote:

> I have a string of state codes (ie ‘MD,PA,VA,WV’) i’m passing from a  
> select list (:allow\_multiple =\> true), in which I’m trying to replace  
> the commas with “‘,’”. I have the following gsub, which works fine in  
> irb, however in Rails it’s returning “‘’,‘’” (double single-quotes).
> 
> states = params[:states].gsub(/,/, “‘,’”)
> 
> Any suggestions?

Your gsub statement looks fine, but my guess is that it’s somehow  
getting called twice, which is why you’re getting the quotes doubled.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [July 20, 2009, 2:03pm UTC](https://www.ruby-forum.com/t/help-with-gsub/172214/3 "2009-07-20T14:03:23Z")

</div>

Marnen Laibow-Koser wrote:

> Brian P. wrote:
> 
> > I have a string of state codes (ie ‘MD,PA,VA,WV’) i’m passing from a  
> > select list (:allow\_multiple =\> true), in which I’m trying to replace  
> > the commas with “‘,’”. I have the following gsub, which works fine in  
> > irb, however in Rails it’s returning “‘’,‘’” (double single-quotes).
> > 
> > states = params[:states].gsub(/,/, “‘,’”)
> > 
> > Any suggestions?
> 
> Your gsub statement looks fine, but my guess is that it’s somehow  
> getting called twice, which is why you’re getting the quotes doubled.
> 
> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

Thanks Maren…I’ll look into it…but, the very next line in my code  
passes the value of states into a find, so not sure what would cause it  
to be called again.

---

<div class="post-metadata">

**Author:** ![parki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b782af/32.png) [@parki](https://www.ruby-forum.com/u/parki)\
**Post date:** [July 21, 2009, 11:28am UTC](https://www.ruby-forum.com/t/help-with-gsub/172214/4 "2009-07-21T11:28:24Z")

</div>

2009/7/20 Brian P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > Any suggestions?  
> > > Thanks Maren…I’ll look into it…but, the very next line in my code  
> > > passes the value of states into a find, so not sure what would cause it  
> > > to be called again.  
> > > –

In this sort of situation I would use ruby-debug to break into the  
code at that point, then you can examine the data in and out of gsub  
and possibly get a clue as to what is happening.

Colin
