简体   繁体   中英

Using gsub to replace String with Symbols in Rails

I have a Rails app that is reading in RSS feeds using the simple_feed gem. However, some of the feeds don't read in correctly - namely, some of the titles have:

`‘`

or

`’`

instead of: "

My titles read in as:

i.title

and I was thinking that gsub can fix this easily, but I was have trouble making it work. I tried:

i.title.gsub(%r[‘]/, '"')

which I'm not even sure would work, but it commented out the line after the # .

Then I tried:

i.title.gsub(%r["‘"]/, '*')

which results in:

C:/Sites/foo/app/views/bar/show.html.erb:20: syntax error, unexpected ','
...( i.title.gsub(%r["‘"]/, '*') )

I haven't really used gsub before, I was trying to work off these examples . Can something please help me figure out what I'm doing wrong?

Try with

i.title.gsub("‘", '"')

or with

i.title.gsub(/‘/, '"')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM