核心扩展字符串变形

String#pluralize

返回复数形式的字符串。如果 count == 1,可以选择接受 count 参数并返回单数形式。还接受 locale 参数以进行特定于语言的复数化。

'post'.pluralize             # => "posts"
'octopus'.pluralize          # => "octopi"
'sheep'.pluralize            # => "sheep"
'words'.pluralize            # => "words"
'the blue mailman'.pluralize # => "the blue mailmen"
'CamelOctopus'.pluralize     # => "CamelOctopi"
'apple'.pluralize(1)         # => "apple"
'apple'.pluralize(2)         # => "apples"
'ley'.pluralize(:es)         # => "leyes"
'ley'.pluralize(1, :es)      # => "ley"

String#singularize

返回字符串的单数形式。接受可选的 locale 参数。

'posts'.singularize            # => "post"
'octopi'.singularize           # => "octopus"
'sheep'.singularize            # => "sheep"
'word'.singularize             # => "word"
'the blue mailmen'.singularize # => "the blue mailman"
'CamelOctopi'.singularize      # => "CamelOctopus"
'leyes'.singularize(:es)       # => "ley"

String#constantize

尝试使用字符串中指定的名称查找声明的常量。当名称不在 CamelCase 中或未初始化时,它会引发一个 NameError

'Module'.constantize  # => Module
'Class'.constantize   # => Class
'blargle'.constantize # => NameError: wrong constant name blargle

String#safe_constantize

执行 constantize 但返回 nil 而不是提升 NameError

'Module'.safe_constantize  # => Module
'Class'.safe_constantize   # => Class
'blargle'.safe_constantize # => nil

String#camelize

默认情况下将字符串转换为 UpperCamelCase,如果给出:lower 作为 param 转换为 lowerCamelCase。

别名:camelcase

注意: 还会将/转换为::,这对于将路径转换为名称空间非常有用。

'active_record'.camelize                # => "ActiveRecord"
'active_record'.camelize(:lower)        # => "activeRecord"
'active_record/errors'.camelize         # => "ActiveRecord::Errors"
'active_record/errors'.camelize(:lower) # => "activeRecord::Errors"

String#titleize

将所有单词大写并替换字符串中的某些字符以创建更好看的标题。

别名:titlecase

'man from the boondocks'.titleize # => "Man From The Boondocks"
'x-men: the last stand'.titleize  # => "X Men: The Last Stand"

String#underscore

从字符串中的表达式创建一个下划线的小写形式。tihuan 的逆转 14。

注意: underscore 也会将::更改为/,以将名称空间转换为路径。

'ActiveModel'.underscore         # => "active_model"
'ActiveModel::Errors'.underscore # => "active_model/errors"

String#dasherize

用字符串中的破折号替换下划线。

'puni_puni'.dasherize # => "puni-puni"

String#demodulize

从字符串中的常量表达式中删除模块部分。

'ActiveRecord::CoreExtensions::String::Inflections'.demodulize # => "Inflections"
'Inflections'.demodulize                                       # => "Inflections"
'::Inflections'.demodulize                                     # => "Inflections"
''.demodulize                                                  # => ''

String#deconstantize

从字符串中的常量表达式中删除最右边的段。

'Net::HTTP'.deconstantize   # => "Net"
'::Net::HTTP'.deconstantize # => "::Net"
'String'.deconstantize      # => ""
'::String'.deconstantize    # => ""
''.deconstantize            # => ""

String#parameterize

替换字符串中的特殊字符,以便它可以用作漂亮URL 的一部分。

"Donald E. Knuth".parameterize # => "donald-e-knuth"

使用:preserve_case 参数保留字符串中字符的大小写。

"Donald E. Knuth".parameterize(preserve_case: true) # => "Donald-E-Knuth"

parameterize 的一个非常常见的用例是覆盖 ActiveRecord 模型的 to_param 方法以支持更具描述性的 url slugs。

class Person < ActiveRecord::Base
  def to_param
    "#{id}-#{name.parameterize}"
  end
end

Person.find(1).to_param # => "1-donald-e-knuth"

String#tableize

为表格名称的模型创建 Rails 表格的名称。将字符串中的最后一个字分开。

'RawScaledScorer'.tableize # => "raw_scaled_scorers"
'ham_and_egg'.tableize     # => "ham_and_eggs"
'fancyCategory'.tableize   # => "fancy_categories"

String#classify

从多个表名返回一个类名字符串,如 Rails 对表名到模型。

'ham_and_eggs'.classify # => "HamAndEgg"
'posts'.classify        # => "Post"

String#humanize

将第一个单词大写,将下划线转换为空格,如果存在则删除尾随的 _id

'employee_salary'.humanize              # => "Employee salary"
'author_id'.humanize                    # => "Author"
'author_id'.humanize(capitalize: false) # => "author"
'_id'.humanize                          # => "Id"

String#upcase_first

仅将第一个字符转换为大写。

'what a Lovely Day'.upcase_first # => "What a Lovely Day"
'w'.upcase_first                 # => "W"
''.upcase_first                  # => ""

String#foreign_key

从类名创建外键名称。通过 false param 禁用在 name 和 id 之间添加 _

'Message'.foreign_key        # => "message_id"
'Message'.foreign_key(false) # => "messageid"
'Admin::Post'.foreign_key    # => "post_id"