class String
String inflections define new methods on the String class to transform names for different purposes.
def camelize(first_letter = :upper)
"active_record/errors".camelize # => "ActiveRecord::Errors"
"active_record".camelize(:lower) # => "activeRecord"
"active_record".camelize # => "ActiveRecord"
+camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
is set to :lower then camelize produces lowerCamelCase.
By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
def camelize(first_letter = :upper) case first_letter when :upper then Inflector.camelize(self, true) when :lower then Inflector.camelize(self, false) end end
def classify
Singular names are not handled correctly.
"posts".classify # => "Post"
"egg_and_hams".classify # => "EggAndHam"
follow +classify+ with +constantize+.)
Note that this returns a string and not a class. (To convert to an actual class
Create a class name from a plural table name like Rails does for table names to models.
def classify Inflector.classify(self) end
def dasherize
Replaces underscores with dashes in the string.
def dasherize Inflector.dasherize(self) end
def demodulize
"ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
Removes the module part from the constant expression in the string.
def demodulize Inflector.demodulize(self) end
def humanize
"employee_salary" # => "Employee salary"
Like +titleize+, this is meant for creating pretty output.
Capitalizes the first word, turns underscores into spaces, and strips '_id'.
def humanize Inflector.humanize(self) end
def pluralize
"the blue mailman".pluralize # => "the blue mailmen"
"words".pluralize # => "words"
"sheep".pluralize # => "sheep"
"octopus".pluralize # => "octopi"
"post".pluralize # => "posts"
Returns the plural form of the word in the string.
def pluralize Inflector.pluralize(self) end
def singularize
"the blue mailmen".singularize # => "the blue mailman"
"word".singularize # => "word"
"sheep".singularize # => "sheep"
"octopi".singularize # => "octopus"
"posts".singularize # => "post"
The reverse of +pluralize+, returns the singular form of a word in a string.
def singularize Inflector.singularize(self) end
def titleize
"man from the boondocks".titleize # => "Man From The Boondocks"
+titleize+ is also aliased as +titlecase+.
used in the Rails internals.
a nicer looking title. +titleize+ is meant for creating pretty output. It is not
Capitalizes all the words and replaces some characters in the string to create
def titleize Inflector.titleize(self) end
def underscore
"ActiveRecord".underscore # => "active_record"
+underscore+ will also change '::' to '/' to convert namespaces to paths.
The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
def underscore Inflector.underscore(self) end