Ruby-on-rails 将骆驼案例转换为红宝石中的下划线案例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1509915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting camel case to underscore case in ruby
提问by Daniel Cukier
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".
返回“camel_case_string”。
...
...
回答by jrhicks
Rails' ActiveSupportadds underscore to the String using the following:
Rails 的 ActiveSupport使用以下内容向 String 添加下划线:
class String
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'_').
gsub(/([a-z\d])([A-Z])/,'_').
tr("-", "_").
downcase
end
end
Then you can do fun stuff:
然后你可以做一些有趣的事情:
"CamelCase".underscore
=> "camel_case"
回答by abo-elleef
回答by kirushik
One-liner Ruby implementation:
单行 Ruby 实现:
class String
# ruby mutation methods have the expectation to return self if a mutation occurred, nil otherwise. (see http://www.ruby-doc.org/core-1.9.3/String.html#method-i-gsub-21)
def to_underscore!
gsub!(/(.)([A-Z])/,'_')
downcase!
end
def to_underscore
dup.tap { |s| s.to_underscore! }
end
end
So "SomeCamelCase".to_underscore # =>"some_camel_case"
所以 "SomeCamelCase".to_underscore # =>"some_camel_case"
回答by Aaditi Jain
There is a Rails inbuilt method called 'underscore' that you can use for this purpose
有一个名为“下划线”的 Rails 内置方法可以用于此目的
"CamelCaseString".underscore #=> "camel_case_string"
The 'underscore' method can typically be considered as inverse of 'camelize'
'underscore' 方法通常可以被认为是 'camelize' 的倒数
回答by Pesto
Here's how Rails does it:
以下是如何Rails的做它:
def underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'_').
gsub(/([a-z\d])([A-Z])/,'_').
tr("-", "_").
downcase
end
回答by Mr. Black
Receiver converted to snake case: http://rubydoc.info/gems/extlib/0.9.15/String#snake_case-instance_method
接收器转换为蛇盒: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)
这是 DataMapper 和 Merb 的支持库。( http://rubygems.org/gems/extlib)
def snake_case
return downcase if match(/\A[A-Z]+\z/)
gsub(/([A-Z]+)([A-Z][a-z])/, '_').
gsub(/([a-z])([A-Z])/, '_').
downcase
end
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
回答by Abram
Check out snakecasefrom Ruby Facets
退房snakecase从红宝石刻面
The following cases are handled, as seen below:
处理以下情况,如下所示:
"SnakeCase".snakecase #=> "snake_case"
"Snake-Case".snakecase #=> "snake_case"
"Snake Case".snakecase #=> "snake_case"
"Snake - Case".snakecase #=> "snake_case"
From: https://github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
来自:https: //github.com/rubyworks/facets/blob/master/lib/core/facets/string/snakecase.rb
class String
# Underscore a string such that camelcase, dashes and spaces are
# replaced by underscores. This is the reverse of {#camelcase},
# albeit not an exact inverse.
#
# "SnakeCase".snakecase #=> "snake_case"
# "Snake-Case".snakecase #=> "snake_case"
# "Snake Case".snakecase #=> "snake_case"
# "Snake - Case".snakecase #=> "snake_case"
#
# Note, this method no longer converts `::` to `/`, in that case
# use the {#pathize} method instead.
def snakecase
#gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'_').
gsub(/([a-z\d])([A-Z])/,'_').
tr('-', '_').
gsub(/\s/, '_').
gsub(/__+/, '_').
downcase
end
#
alias_method :underscore, :snakecase
# TODO: Add *separators to #snakecase, like camelcase.
end
回答by Andres Ehrenpreis
Short oneliner for CamelCases when you have spaces also included (doesn't work correctly if you have a word inbetween with small starting-letter):
当您还包含空格时,CamelCases 的短单行(如果中间有一个带有小起始字母的单词,则无法正常工作):
a = "Test String"
a.gsub(' ', '').underscore
=> "test_string"
回答by Arman Petrosyan
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
如果有人在寻找需要将下划线应用于带空格的字符串并希望将它们转换为下划线的情况下,您可以使用这样的东西
'your String will be converted To underscore'.parameterize.underscore
#your_string_will_be_converted_to_underscore
Or just use .parameterize('_') but keep in mind that this one is deprecated
或者只使用 .parameterize('_') 但请记住,不推荐使用这个
'your String will be converted To underscore'.parameterize('_')
#your_string_will_be_converted_to_underscore
回答by rplaurindo
I would like this:
我想要这个:
class String
# \n returns the capture group of "n" index
def snikize
self.gsub(/::/, '/')
.gsub(/([a-z\d])([A-Z])/, "_")
.downcase
end
# or
def snikize
self.gsub(/::/, '/')
.gsub(/([a-z\d])([A-Z])/) do
"#{}_#{}"
end
.downcase
end
end
Monkey patch of Stringclass. There are class that begin with two or more letters in uppercase.
猴子补丁String类。有些类以两个或多个大写字母开头。

