核心擴充套件字串變形

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"