简体   繁体   English

在Rails中使用gsub将Symbol替换为String

[英]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. 我有一个正在使用simple_feed gem读取RSS feed的Rails应用程序。 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. 我当时以为gsub可以轻松解决这个问题,但是我很难让它起作用。 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 . 之前我还没有真正使用过gsub,我试图研究这些示例 Can something please help me figure out what I'm doing wrong? 可以帮我弄清楚我在做什么错吗?

Try with 试试看

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

or with 或搭配

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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