[ruby-on-rails] 루비에서 낙타 케이스를 밑줄 케이스로 변환

낙타 문자 문자열을 밑줄로 구분 된 문자열로 변환하는 준비된 기능이 있습니까?

나는 이런 것을 원한다.

"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" 

‘밑줄’방법은 일반적으로 ‘동기화’의 역으로 ​​간주 될 수 있습니다.


답변

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/gems/extlib/0.9.15/String#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"


답변

Ruby Facets 에서 뱀 장식 확인

다음과 같은 경우에 처리됩니다.

"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