是否有任何现成的函数转换驼峰大小写字符串为下划线分隔字符串?

我想要这样的东西:

"CamelCaseString".to_underscore      

返回“camel_case_string”。

...


当前回答

ruby内核本身不支持将字符串从(上)驼峰格式转换为(也称为pascal格式)下划线格式(也称为蛇形格式)。

因此,您需要创建自己的实现,或者使用现有的gem。

有一个叫做lucky_case的小红宝石宝石,它允许你将一个字符串从任何一个支持的10+ case轻松转换为另一个case:

require 'lucky_case'

# convert to snake case string
LuckyCase.snake_case('CamelCaseString')      # => 'camel_case_string'
# or the opposite way
LuckyCase.pascal_case('camel_case_string')   # => 'CamelCaseString'

如果你想,你甚至可以猴子修补String类:

require 'lucky_case/string'

'CamelCaseString'.snake_case  # => 'camel_case_string'
'CamelCaseString'.snake_case! # => 'camel_case_string' and overwriting original

请查看官方存储库以获得更多示例和文档:

https://github.com/magynhard/lucky_case

其他回答

我有麻烦运行“CamelCaseString”。在rake任务中使用下划线。这帮助了我:

ActiveSupport::Inflector.underscore "CamelCaseString"
=> "camel_case_string"

当然,您需要使用ActiveSupport

我想要这样:

class String

  # \n returns the capture group of "n" index
  def snakize
    self.gsub(/::/, '/')
    .gsub(/([a-z\d])([A-Z])/, "\1_\2")
    .downcase
  end

  # or

  def snakize
    self.gsub(/::/, '/')
    .gsub(/([a-z\d])([A-Z])/) do
      "#{$1}_#{$2}"
    end
    .downcase
  end

end

猴子补丁的字符串类。有些类以两个或多个大写字母开头。

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"

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

接收器转换为蛇案: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"