Rubyでキャメルケースをアンダースコアケースに変換する
-
19-09-2019 - |
質問
キャメルケースの文字列をアンダースコアで区切られた文字列に変換する既製の関数はありますか?
次のようなものが欲しいです:
"CamelCaseString".to_underscore
「camel_case_string」を返します。
...
解決
Railsのactivesupportのの 次を使用して文字列に下線を追加します:
class String
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
そして、あなたが楽しいものを行うことができます:
"CamelCase".underscore
=> "camel_case"
他のヒント
あなたが使用することができます。
"CamelCasedName".tableize.singularize
それとも、
"CamelCasedName".underscore
両方のオプションの方法"camel_cased_name"
が得られます。あなたはここにの詳細は、それを確認することができます。
ワンライナー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])/,'\1_\2')
downcase!
end
def to_underscore
dup.tap { |s| s.to_underscore! }
end
end
だから、"SomeCamelCase".to_underscore # =>"some_camel_case"
あなたは、この目的のために使用することができます「アンダースコア」と呼ばれるRailsの作り付けの方法があります。
"CamelCaseString".underscore #=> "camel_case_string"
「下線」方法は、典型的には
「camelize」の逆数と考えることができますは、ここで Railsはそれのない方法です
def underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
レシーバは、ヘビのケースに変換: http://rubydoc.info/宝石/ EXTLIB / 0.9.15 /文字列#1 snake_case-instance_methodする
このは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])/, '\1_\2').
gsub(/([a-z])([A-Z])/, '\1_\2').
downcase
end
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
チェックアウト 蛇のケース から ルビーのファセット
以下に示すように、次のケースが処理されます。
"SnakeCase".snakecase #=> "snake_case"
"Snake-Case".snakecase #=> "snake_case"
"Snake Case".snakecase #=> "snake_case"
"Snake - Case".snakecase #=> "snake_case"
から: 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])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr('-', '_').
gsub(/\s/, '_').
gsub(/__+/, '_').
downcase
end
#
alias_method :underscore, :snakecase
# TODO: Add *separators to #snakecase, like camelcase.
end
あなたは、スペースにも(あなたは小さな開始文字とわたってるしきの単語を持っている場合、正しく動作しません)が含まれているキャメルケースのための短い1つのライナーます:
a = "Test String"
a.gsub(' ', '').underscore
=> "test_string"
私はこれをしたいと思います:
class String
# \n returns the capture group of "n" index
def snikize
self.gsub(/::/, '/')
.gsub(/([a-z\d])([A-Z])/, "\1_\2")
.downcase
end
# or
def snikize
self.gsub(/::/, '/')
.gsub(/([a-z\d])([A-Z])/) do
"#{$1}_#{$2}"
end
.downcase
end
end
String
クラスのモンキーパッチ。大文字で二つ以上の文字で始まるクラスがあります。