在 ruby 中将驼峰大小写转换为下划线大小写
是否有任何现成的函数可以将驼峰式大小写字符串转换为下划线分隔的字符串?
我想要这样的东西:
"CamelCaseString".to_underscore
返回“camel_case_string”。
...
Is there any ready function which converts camel case Strings into underscore separated string?
I want something like this:
"CamelCaseString".to_underscore
to return "camel_case_string".
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Rails 的 ActiveSupport
使用以下命令向字符串添加下划线:
然后你可以做一些有趣的事情:
Rails' ActiveSupport
adds underscore to the String using the following:
Then you can do fun stuff:
您可以使用
或仅使用
两种方式都会产生
“camel_cased_name”
。您可以在此处查看更多详细信息。您需要
active_support/inflector
才能正常工作(请参阅评论)。You can use
Or just
Both options ways will yield
"camel_cased_name"
. You can check more details it here.You need to require
active_support/inflector
for it to work (see comments).单行 Ruby 实现:
所以
"SomeCamelCase".to_underscore # =>"some_camel_case"
One-liner Ruby implementation:
So
"SomeCamelCase".to_underscore # =>"some_camel_case"
Rails 有一个名为“underscore”的内置方法,您可以将其用于此目的。
“underscore”方法通常可以被视为“camelize”的逆方法
There is a Rails inbuilt method called 'underscore' that you can use for this purpose
The 'underscore' method can typically be considered as inverse of 'camelize'
如果有人寻找需要将下划线应用于带有空格的字符串并希望将它们转换为下划线的情况,您可以使用类似这样的东西
或者只使用 .parameterize('_') 但请记住,这个已被弃用
In case someone looking for case when he need to apply underscore to string with spaces and want to convert them to underscores as well you can use something like this
Or just use .parameterize('_') but keep in mind that this one is deprecated
以下是 Rails 的做法:
Here's how Rails does it:
当您还包含空格时,CamelCases 的短单行符(如果中间有一个带有小起始字母的单词,则无法正常工作):
编辑: 正如 @dft 所指出的,那么此方法不是一部分Ruby 和 Rails 的区别。
Short oneliner for CamelCases when you have spaces also included (doesn't work correctly if you have a word inbetween with small starting-letter):
EDIT: As pointed out by @dft then this method is not part of Ruby but Rails.
查看来自 Ruby Facets 的 snakecase
处理以下情况,如下所示:
来自: https://github .com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
Check out snakecase from Ruby Facets
The following cases are handled, as seen below:
From: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
接收器转换为蛇形案例: http://rubydoc.info/gems/extlib /0.9.15/String#snake_case-instance_method
这是 DataMapper 和 Merb 的支持库。 (http://rubygems.org/gems/extlib)
Receiver converted to snake case: http://rubydoc.info/gems/extlib/0.9.15/String#snake_case-instance_method
This is the Support library for DataMapper and Merb. (http://rubygems.org/gems/extlib)
我在 rake 任务中运行
"CamelCaseString".underscore
时遇到问题。这对我有帮助:当然,您需要需要 ActiveSupport
I had trouble running
"CamelCaseString".underscore
in a rake task. This helped me:Of course, you need to require ActiveSupport
ruby 核心本身不支持将字符串从(大写)驼峰式大小写(也称为帕斯卡大小写)转换为下划线大小写(也称为蛇形大小写)。
因此,您需要自己实现或使用现有的 gem。
有一个名为 lucky_case 的小型 ruby gem,它允许您轻松地将字符串从 10 多种支持的情况中的任何一种转换为另一种情况:
,您甚至可以修补 String 类:
如果您愿意 查看官方存储库以获取更多示例和文档:
https://github.com/magynhard/lucky_case
The ruby core itself has no support to convert a string from (upper) camel case to (also known as pascal case) to underscore (also known as snake case).
So you need either to make your own implementation or use an existing gem.
There is a small ruby gem called lucky_case which allows you to convert a string from any of the 10+ supported cases to another case easily:
You can even monkey patch the String class if you want to:
Have a look at the offical repository for more examples and documentation:
https://github.com/magynhard/lucky_case
我想要这样:
String
类的猴子补丁。有些类以两个或多个大写字母开头。I would like this:
Monkey patch of
String
class. There are class that begin with two or more letters in uppercase.