我知道你可以这样做:

"SomeWordHere".underscore.gsub("_", " ")

得到“这里的一些词”。
我想对于这么简单的事情来说,这可能有点过分了。有没有更有效的方法(可能是内置的方法?)把“some word here”转换成“somewordhere”?

最佳答案

方法下划线和人性化是为表、类/包名称等之间的转换而设计的。最好使用自己的代码来进行替换,以避免意外。见评论。

"SomeWordHere".underscore => "some_word_here"

"SomeWordHere".underscore.humanize => "Some word here"

"SomeWordHere".underscore.humanize.downcase => "some word here"

关于ruby-on-rails - Ruby on Rails:将“SomeWordHere”转换为“此处有字”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2842444/

10-10 04:22