简体   繁体   English

如何在rails上的ruby中添加前缀并删除双引号?

[英]How to add prefix and remove double quotes from the value in ruby on rails?

I have a value called "FooBar" . 我有一个名为"FooBar"的值。 I want to replace this text with the quotes to Enr::Rds::FooBar without quotes. 我想用没有引号的Enr::Rds::FooBar的引号替换这个文本。

Update: 更新:

For example, @question.answer_model gives the value "FooBar" (with the quotes) 例如, @question.answer_model给出值"FooBar" (带引号)

I am a newbie and somebody please refer me how would i start to know about regex? 我是新手,有人请你告诉我如何开始了解正则表达式? What is the best way to practice in online? 在线练习的最佳方式是什么?

Since you want to: a) drop the quotes and b) prepend FooBar with Enr::Rds:: , I would suggest you preform exactly what is intended, literally: 既然你想要:a)删除引号和b)用Enr::Rds::前置FooBar ,我建议你按照字面意思预先FooBar预期的内容:

'"FooBar"'.delete('"').gsub(/\A/, "Enr::Rds::")
# => "Enr::Rds::FooBar" 

I think you are trying to convert a string to a constant. 我想你正在尝试将字符串转换为常量。 try the following 尝试以下方法

"Enr::Rds::#{@question.answer_model.gsub('"', '')}".constantize.find(...)

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

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