本文介绍了Ruby on Rails:转换“SomeWordHere"到“这里有一些词"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以这样做:

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

得到这里的一些词".

我认为这对于这么简单的事情来说可能有点太多了.有没有更有效的方法(也许是内置方法?)将SomeWordHere"转换为some word here"?

I thought that might be a little too much for something so simple. Is there a more efficient way (maybe a built-in method?) to convert "SomeWordHere" to "some word here"?

推荐答案

underscorehumanize 方法专为表格、类/包名称等之间的转换而设计.您最好使用自己的代码进行替换以避免惊喜.查看评论.

The methods underscore and humanize are designed for conversions between tables, class/package names, etc. You are better off using your own code to do the replacement to avoid surprises. See comments.

"SomeWordHere".underscore => "some_word_here"

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

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

这篇关于Ruby on Rails:转换“SomeWordHere"到“这里有一些词"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 21:34