class String
Issue: github.com/rails/rails/issues/1526<br><br>to prevent loading several dependencies including I18n gem.
This is an adapted version of active_support/core_ext/string/inflections.rb
#
def self.colors
def self.colors @_colors ||= { :clear => 0, :bold => 1, :black => 30, :red => 31, :green => 32, :yellow => 33, :blue => 34, :magenta => 35, :cyan => 36, :white => 37 } end
def camelize(first_letter = :upper)
"active_record/errors".camelize(:lower) # => "activeRecord::Errors"
"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 ActiveSupport::Inflector.camelize(self, true) when :lower then ActiveSupport::Inflector.camelize(self, false) end end
def classify
"business".classify # => "Busines"
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 ActiveSupport::Inflector.classify(self) end
def constantize
"Class".constantize # => Class
"Module".constantize # => Module
or is not initialized.
in the string. It raises a NameError when the name is not in CamelCase
+constantize+ tries to find a declared constant with the name specified
#
def constantize ActiveSupport::Inflector.constantize(self) end
def pluralize
"CamelOctopus".pluralize # => "CamelOctopi"
"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 ActiveSupport::Inflector.pluralize(self) end
def singularize
"CamelOctopi".singularize # => "CamelOctopus"
"the blue mailmen".singularize # => "the blue mailman"
"words".singularize # => "word"
"sheep".singularize # => "sheep"
"octopi".singularize # => "octopus"
"posts".singularize # => "post"
Returns the singular form of the word in the string.
#
def singularize ActiveSupport::Inflector.singularize(self) end
def undent
def undent gsub(/^.{#{slice(/^ +/).size}}/, '') end
def underscore
"ActiveRecord::Errors".underscore # => active_record/errors
"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 ActiveSupport::Inflector.underscore(self) end