class ActiveSupport::Inflector::Inflections
before any of the rules that may already have been loaded.
singularization rules that is runs. This guarantees that your rules run
rule for octopus will now be the first of the pluralization and
New rules are added at the top. So in the example above, the irregular
end
inflect.uncountable ‘equipment’
inflect.irregular ‘octopus’, ‘octopi’
inflect.singular /^(ox)en/i, ‘1’
inflect.plural /^(ox)$/i, ‘12en’
ActiveSupport::Inflector.inflections(:en) do |inflect|
default locale is :en
. Only rules for English are provided.
an optional locale, rules for other languages can be specified. The
which can then be used to specify additional inflection rules. If passed
A singleton instance of this class is yielded by Inflector.inflections,
def self.instance(locale = :en)
def self.instance(locale = :en) @__instance__[locale] ||= new end
def acronym(word)
underscore 'McDonald' # => 'mcdonald'
acronym 'McDonald'
camelize 'restful_controller' # => 'RESTfulController'
camelize 'restful' # => 'RESTful'
titleize 'RESTfulController' # => 'RESTful Controller'
underscore 'RESTfulController' # => 'restful_controller'
underscore 'RESTful' # => 'restful'
acronym 'RESTful'
restriction is that the word must begin with a capital letter.
otherwise needs to maintain a non-standard capitalization. The only
+acronym+ may be used to specify any word that contains an acronym or
camelize(pluralize('api')) # => 'APIs'
acronym 'APIs'
camelize(pluralize('api')) # => 'Apis'
acronym 'API'
form as an acronym as well:
pluralized result. To work around this, you must specify the pluralized
recognized, since the acronym will not occur as a delimited unit in the
Note: Acronyms that are passed to +pluralize+ will no longer be
underscore 'HTTPS' # => 'https'
camelize 'https' # => 'HTTPS'
acronym 'HTTPS'
underscore 'HTTPS' # => 'http_s', not 'https'
camelize 'https' # => 'Https', not 'HTTPs'
camelize 'my_http_delimited' # => 'MyHTTPDelimited'
acronym 'HTTP'
another word for conversions to recognize it:
The acronym, however, must occur as a delimited unit and not be part of
underscore 'MyHTML' # => 'my_html'
camelize 'html' # => 'HTML'
titleize 'html' # => 'HTML'
acronym 'HTML'
into a non-delimited single lowercase word when passed to +underscore+.
the acronym when titleized or humanized, and will convert the acronym
+titleize+. A camelized string that contains the acronym will maintain
will retain the acronym when passed to +camelize+, +humanize+, or
in a camelized string. An underscore string that contains the acronym
Specifies a new acronym. An acronym must be specified as it will appear
def acronym(word) @acronyms[word.downcase] = word @acronym_regex = /#{@acronyms.values.join("|")}/ end
def clear(scope = :all)
clear :all
:humans.
options are: :plurals, :singulars, :uncountables,
:all). Give the scope as a symbol of the inflection type, the
Clears the loaded inflections within a given scope (default is
def clear(scope = :all) case scope when :all @plurals, @singulars, @uncountables, @humans = [], [], Uncountables.new, [] else instance_variable_set "@#{scope}", [] end end
def human(rule, replacement)
human /_cnt$/i, '\1_count'
'The name', not 'the_name').
string is used, the human form should be specified as desired (example:
the normal humanize formatting is called after the replacement. When a
by a string mapping. When using a regular expression based replacement,
Specifies a humanized form of a string by a regular expression rule or
def human(rule, replacement) @humans.prepend([rule, replacement]) end
def initialize
def initialize @plurals, @singulars, @uncountables, @humans, @acronyms, @acronym_regex = [], [], Uncountables.new, [], {}, /(?=a)b/ end
def initialize_dup(orig) # :nodoc:
Private, for the test suite.
def initialize_dup(orig) # :nodoc: %w(plurals singulars uncountables humans acronyms acronym_regex).each do |scope| instance_variable_set("@#{scope}", orig.send(scope).dup) end end
def irregular(singular, plural)
irregular 'octopus', 'octopi'
plural form.
regular expressions. You simply pass the irregular in singular and
singularization at the same time. This can only be used for strings, not
Specifies a new irregular that applies to both pluralization and
def irregular(singular, plural) @uncountables.delete(singular) @uncountables.delete(plural) s0 = singular[0] srest = singular[1..-1] p0 = plural[0] prest = plural[1..-1] if s0.upcase == p0.upcase plural(/(#{s0})#{srest}$/i, '\1' + prest) plural(/(#{p0})#{prest}$/i, '\1' + prest) singular(/(#{s0})#{srest}$/i, '\1' + srest) singular(/(#{p0})#{prest}$/i, '\1' + srest) else plural(/#{s0.upcase}(?i)#{srest}$/, p0.upcase + prest) plural(/#{s0.downcase}(?i)#{srest}$/, p0.downcase + prest) plural(/#{p0.upcase}(?i)#{prest}$/, p0.upcase + prest) plural(/#{p0.downcase}(?i)#{prest}$/, p0.downcase + prest) singular(/#{s0.upcase}(?i)#{srest}$/, s0.upcase + srest) singular(/#{s0.downcase}(?i)#{srest}$/, s0.downcase + srest) singular(/#{p0.upcase}(?i)#{prest}$/, s0.upcase + srest) singular(/#{p0.downcase}(?i)#{prest}$/, s0.downcase + srest) end end
def plural(rule, replacement)
always be a string that may include references to the matched data from
either be a string or a regular expression. The replacement should
Specifies a new pluralization rule and its replacement. The rule can
def plural(rule, replacement) @uncountables.delete(rule) if rule.is_a?(String) @uncountables.delete(replacement) @plurals.prepend([rule, replacement]) end
def singular(rule, replacement)
always be a string that may include references to the matched data from
either be a string or a regular expression. The replacement should
Specifies a new singularization rule and its replacement. The rule can
def singular(rule, replacement) @uncountables.delete(rule) if rule.is_a?(String) @uncountables.delete(replacement) @singulars.prepend([rule, replacement]) end
def uncountable(*words)
uncountable 'money', 'information'
uncountable 'money'
Specifies words that are uncountable and should not be inflected.
def uncountable(*words) @uncountables.add(words) end