1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Update docs of String methods using ActiveSupport::Inflector [ci skip] (#37890)

This commit is contained in:
Shu Fujita 2019-12-07 12:11:15 +09:00 committed by Ryuta Kamizono
parent d0dd199206
commit 97b0833458

View file

@ -30,6 +30,8 @@ class String
# 'apple'.pluralize(2) # => "apples" # 'apple'.pluralize(2) # => "apples"
# 'ley'.pluralize(:es) # => "leyes" # 'ley'.pluralize(:es) # => "leyes"
# 'ley'.pluralize(1, :es) # => "ley" # 'ley'.pluralize(1, :es) # => "ley"
#
# See ActiveSupport::Inflector.pluralize.
def pluralize(count = nil, locale = :en) def pluralize(count = nil, locale = :en)
locale = count if count.is_a?(Symbol) locale = count if count.is_a?(Symbol)
if count == 1 if count == 1
@ -53,28 +55,34 @@ class String
# 'the blue mailmen'.singularize # => "the blue mailman" # 'the blue mailmen'.singularize # => "the blue mailman"
# 'CamelOctopi'.singularize # => "CamelOctopus" # 'CamelOctopi'.singularize # => "CamelOctopus"
# 'leyes'.singularize(:es) # => "ley" # 'leyes'.singularize(:es) # => "ley"
#
# See ActiveSupport::Inflector.singularize.
def singularize(locale = :en) def singularize(locale = :en)
ActiveSupport::Inflector.singularize(self, locale) ActiveSupport::Inflector.singularize(self, locale)
end end
# +constantize+ tries to find a declared constant with the name specified # +constantize+ tries to find a declared constant with the name specified
# in the string. It raises a NameError when the name is not in CamelCase # in the string. It raises a NameError when the name is not in CamelCase
# or is not initialized. See ActiveSupport::Inflector.constantize # or is not initialized.
# #
# 'Module'.constantize # => Module # 'Module'.constantize # => Module
# 'Class'.constantize # => Class # 'Class'.constantize # => Class
# 'blargle'.constantize # => NameError: wrong constant name blargle # 'blargle'.constantize # => NameError: wrong constant name blargle
#
# See ActiveSupport::Inflector.constantize.
def constantize def constantize
ActiveSupport::Inflector.constantize(self) ActiveSupport::Inflector.constantize(self)
end end
# +safe_constantize+ tries to find a declared constant with the name specified # +safe_constantize+ tries to find a declared constant with the name specified
# in the string. It returns +nil+ when the name is not in CamelCase # in the string. It returns +nil+ when the name is not in CamelCase
# or is not initialized. See ActiveSupport::Inflector.safe_constantize # or is not initialized.
# #
# 'Module'.safe_constantize # => Module # 'Module'.safe_constantize # => Module
# 'Class'.safe_constantize # => Class # 'Class'.safe_constantize # => Class
# 'blargle'.safe_constantize # => nil # 'blargle'.safe_constantize # => nil
#
# See ActiveSupport::Inflector.safe_constantize.
def safe_constantize def safe_constantize
ActiveSupport::Inflector.safe_constantize(self) ActiveSupport::Inflector.safe_constantize(self)
end end
@ -88,6 +96,10 @@ class String
# 'active_record'.camelize(:lower) # => "activeRecord" # 'active_record'.camelize(:lower) # => "activeRecord"
# 'active_record/errors'.camelize # => "ActiveRecord::Errors" # 'active_record/errors'.camelize # => "ActiveRecord::Errors"
# 'active_record/errors'.camelize(:lower) # => "activeRecord::Errors" # 'active_record/errors'.camelize(:lower) # => "activeRecord::Errors"
#
# +camelize+ is also aliased as +camelcase+.
#
# See ActiveSupport::Inflector.camelize.
def camelize(first_letter = :upper) def camelize(first_letter = :upper)
case first_letter case first_letter
when :upper when :upper
@ -108,11 +120,13 @@ class String
# optional parameter +keep_id_suffix+ to true. # optional parameter +keep_id_suffix+ to true.
# By default, this parameter is false. # By default, this parameter is false.
# #
# +titleize+ is also aliased as +titlecase+.
#
# 'man from the boondocks'.titleize # => "Man From The Boondocks" # 'man from the boondocks'.titleize # => "Man From The Boondocks"
# 'x-men: the last stand'.titleize # => "X Men: The Last Stand" # 'x-men: the last stand'.titleize # => "X Men: The Last Stand"
# 'string_ending_with_id'.titleize(keep_id_suffix: true) # => "String Ending With Id" # 'string_ending_with_id'.titleize(keep_id_suffix: true) # => "String Ending With Id"
#
# +titleize+ is also aliased as +titlecase+.
#
# See ActiveSupport::Inflector.titleize.
def titleize(keep_id_suffix: false) def titleize(keep_id_suffix: false)
ActiveSupport::Inflector.titleize(self, keep_id_suffix: keep_id_suffix) ActiveSupport::Inflector.titleize(self, keep_id_suffix: keep_id_suffix)
end end
@ -124,6 +138,8 @@ class String
# #
# 'ActiveModel'.underscore # => "active_model" # 'ActiveModel'.underscore # => "active_model"
# 'ActiveModel::Errors'.underscore # => "active_model/errors" # 'ActiveModel::Errors'.underscore # => "active_model/errors"
#
# See ActiveSupport::Inflector.underscore.
def underscore def underscore
ActiveSupport::Inflector.underscore(self) ActiveSupport::Inflector.underscore(self)
end end
@ -131,6 +147,8 @@ class String
# Replaces underscores with dashes in the string. # Replaces underscores with dashes in the string.
# #
# 'puni_puni'.dasherize # => "puni-puni" # 'puni_puni'.dasherize # => "puni-puni"
#
# See ActiveSupport::Inflector.dasherize.
def dasherize def dasherize
ActiveSupport::Inflector.dasherize(self) ActiveSupport::Inflector.dasherize(self)
end end
@ -142,6 +160,8 @@ class String
# '::Inflections'.demodulize # => "Inflections" # '::Inflections'.demodulize # => "Inflections"
# ''.demodulize # => '' # ''.demodulize # => ''
# #
# See ActiveSupport::Inflector.demodulize.
#
# See also +deconstantize+. # See also +deconstantize+.
def demodulize def demodulize
ActiveSupport::Inflector.demodulize(self) ActiveSupport::Inflector.demodulize(self)
@ -155,6 +175,8 @@ class String
# '::String'.deconstantize # => "" # '::String'.deconstantize # => ""
# ''.deconstantize # => "" # ''.deconstantize # => ""
# #
# See ActiveSupport::Inflector.deconstantize.
#
# See also +demodulize+. # See also +demodulize+.
def deconstantize def deconstantize
ActiveSupport::Inflector.deconstantize(self) ActiveSupport::Inflector.deconstantize(self)
@ -192,6 +214,8 @@ class String
# #
# <%= link_to(@person.name, person_path) %> # <%= link_to(@person.name, person_path) %>
# # => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a> # # => <a href="/person/1-Donald-E-Knuth">Donald E. Knuth</a>
#
# See ActiveSupport::Inflector.parameterize.
def parameterize(separator: "-", preserve_case: false, locale: nil) def parameterize(separator: "-", preserve_case: false, locale: nil)
ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case, locale: locale) ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case, locale: locale)
end end
@ -202,6 +226,8 @@ class String
# 'RawScaledScorer'.tableize # => "raw_scaled_scorers" # 'RawScaledScorer'.tableize # => "raw_scaled_scorers"
# 'ham_and_egg'.tableize # => "ham_and_eggs" # 'ham_and_egg'.tableize # => "ham_and_eggs"
# 'fancyCategory'.tableize # => "fancy_categories" # 'fancyCategory'.tableize # => "fancy_categories"
#
# See ActiveSupport::Inflector.tableize.
def tableize def tableize
ActiveSupport::Inflector.tableize(self) ActiveSupport::Inflector.tableize(self)
end end
@ -212,6 +238,8 @@ class String
# #
# 'ham_and_eggs'.classify # => "HamAndEgg" # 'ham_and_eggs'.classify # => "HamAndEgg"
# 'posts'.classify # => "Post" # 'posts'.classify # => "Post"
#
# See ActiveSupport::Inflector.classify.
def classify def classify
ActiveSupport::Inflector.classify(self) ActiveSupport::Inflector.classify(self)
end end
@ -233,6 +261,8 @@ class String
# 'author_id'.humanize(capitalize: false) # => "author" # 'author_id'.humanize(capitalize: false) # => "author"
# '_id'.humanize # => "Id" # '_id'.humanize # => "Id"
# 'author_id'.humanize(keep_id_suffix: true) # => "Author Id" # 'author_id'.humanize(keep_id_suffix: true) # => "Author Id"
#
# See ActiveSupport::Inflector.humanize.
def humanize(capitalize: true, keep_id_suffix: false) def humanize(capitalize: true, keep_id_suffix: false)
ActiveSupport::Inflector.humanize(self, capitalize: capitalize, keep_id_suffix: keep_id_suffix) ActiveSupport::Inflector.humanize(self, capitalize: capitalize, keep_id_suffix: keep_id_suffix)
end end
@ -242,6 +272,8 @@ class String
# 'what a Lovely Day'.upcase_first # => "What a Lovely Day" # 'what a Lovely Day'.upcase_first # => "What a Lovely Day"
# 'w'.upcase_first # => "W" # 'w'.upcase_first # => "W"
# ''.upcase_first # => "" # ''.upcase_first # => ""
#
# See ActiveSupport::Inflector.upcase_first.
def upcase_first def upcase_first
ActiveSupport::Inflector.upcase_first(self) ActiveSupport::Inflector.upcase_first(self)
end end
@ -253,6 +285,8 @@ class String
# 'Message'.foreign_key # => "message_id" # 'Message'.foreign_key # => "message_id"
# 'Message'.foreign_key(false) # => "messageid" # 'Message'.foreign_key(false) # => "messageid"
# 'Admin::Post'.foreign_key # => "post_id" # 'Admin::Post'.foreign_key # => "post_id"
#
# See ActiveSupport::Inflector.foreign_key.
def foreign_key(separate_class_name_and_id_with_underscore = true) def foreign_key(separate_class_name_and_id_with_underscore = true)
ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore) ActiveSupport::Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
end end