module ActiveSupport::Inflector

def apply_inflections(word, rules)

apply_inflections("posts", inflections.singulars) # => "post"
apply_inflections("post", inflections.plurals) # => "posts"
Examples:

Applies inflection rules for +singularize+ and +pluralize+.
def apply_inflections(word, rules)
  result = word.to_s.dup
  if word.empty? || inflections.uncountables.any? { |inflection| result =~ /\b#{inflection}\Z/i }
    result
  else
    rules.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
    result
  end
end

def camelize(term, uppercase_first_letter = true)

"SSLError".underscore.camelize # => "SslError"

though there are cases where that does not hold:
As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,

"active_model/errors".camelize(:lower) # => "activeModel::Errors"
"active_model/errors".camelize # => "ActiveModel::Errors"
"active_model".camelize(:lower) # => "activeModel"
"active_model".camelize # => "ActiveModel"
Examples:

+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(term, uppercase_first_letter = true)
  string = term.to_s
  if uppercase_first_letter
    string = string.sub(/^[a-z\d]*/) { inflections.acronyms[$&] || $&.capitalize }
  else
    string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { $&.downcase }
  end
  string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }.gsub('/', '::')
end

def classify(table_name)

"business".classify # => "Busines"
Singular names are not handled correctly:

"posts".classify # => "Post"
"egg_and_hams".classify # => "EggAndHam"
Examples:

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(table_name)
  # strip out any leading schema name
  camelize(singularize(table_name.to_s.sub(/.*\./, '')))
end

def const_regexp(camel_cased_word) #:nodoc:

:nodoc:
For instance, Foo::Bar::Baz will generate Foo(::Bar(::Baz)?)?
Mount a regular expression that will match part by part of the constant.
def const_regexp(camel_cased_word) #:nodoc:
  parts = camel_cased_word.split("::")
  last  = parts.pop
  parts.reverse.inject(last) do |acc, part|
    part.empty? ? acc : "#{part}(::#{acc})?"
  end
end

def constantize(camel_cased_word)

unknown.
NameError is raised when the name is not in CamelCase or the constant is

end
"C".constantize # => 'outside', same as ::C
C # => 'inside'
C = 'inside'
module M
C = 'outside'

it starts with "::" or not. No lexical context is taken into account:
The name is assumed to be the one of a top-level constant, no matter whether

"Test::Unit".constantize # => Test::Unit
"Module".constantize # => Module

Tries to find a constant with the name specified in the argument string:
def constantize(camel_cased_word)
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?
  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

def constantize(camel_cased_word) #:nodoc:

:nodoc:
def constantize(camel_cased_word) #:nodoc:
  names = camel_cased_word.split('::')
  names.shift if names.empty? || names.first.empty?
  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

def dasherize(underscored_word)

"puni_puni" # => "puni-puni"
Example:

Replaces underscores with dashes in the string.
def dasherize(underscored_word)
  underscored_word.gsub(/_/, '-')
end

def deconstantize(path)

See also +demodulize+.

"".deconstantize # => ""
"::String".deconstantize # => ""
"String".deconstantize # => ""
"::Net::HTTP".deconstantize # => "::Net"
"Net::HTTP".deconstantize # => "Net"

Removes the rightmost segment from the constant expression in the string:
def deconstantize(path)
  path.to_s[0...(path.rindex('::') || 0)] # implementation based on the one in facets' Module#spacename
end

def demodulize(path)

See also +deconstantize+.

"Inflections".demodulize # => "Inflections"
"ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"

Removes the module part from the expression in the string:
def demodulize(path)
  path = path.to_s
  if i = path.rindex('::')
    path[(i+2)..-1]
  else
    path
  end
end

def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)

"Admin::Post".foreign_key # => "post_id"
"Message".foreign_key(false) # => "messageid"
"Message".foreign_key # => "message_id"
Examples:

the method should put '_' between the name and 'id'.
+separate_class_name_and_id_with_underscore+ sets whether
Creates a foreign key name from a class name.
def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
  underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
end

def humanize(lower_case_and_underscored_word)

"author_id" # => "Author"
"employee_salary" # => "Employee salary"
Examples:

trailing "_id", if any. Like +titleize+, this is meant for creating pretty output.
Capitalizes the first word and turns underscores into spaces and strips a
def humanize(lower_case_and_underscored_word)
  result = lower_case_and_underscored_word.to_s.dup
  inflections.humans.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
  result.gsub!(/_id$/, "")
  result.gsub!(/_/, ' ')
  result.gsub(/([a-z\d]*)/i) { |match|
    "#{inflections.acronyms[match] || match.downcase}"
  }.gsub(/^\w/) { $&.upcase }
end

def inflections

end
inflect.uncountable "rails"
ActiveSupport::Inflector.inflections do |inflect|
Example:

inflector rules.
Yields a singleton instance of Inflector::Inflections so you can specify additional
def inflections
  if block_given?
    yield Inflections.instance
  else
    Inflections.instance
  end
end

def ordinalize(number)

ordinalize(-1021) # => "-1021st"
ordinalize(-11) # => "-11th"
ordinalize(1003) # => "1003rd"
ordinalize(1002) # => "1002nd"
ordinalize(2) # => "2nd"
ordinalize(1) # => "1st"
Examples:

ordered sequence such as 1st, 2nd, 3rd, 4th.
Turns a number into an ordinal string used to denote the position in an
def ordinalize(number)
  if (11..13).include?(number.to_i.abs % 100)
    "#{number}th"
  else
    case number.to_i.abs % 10
      when 1; "#{number}st"
      when 2; "#{number}nd"
      when 3; "#{number}rd"
      else    "#{number}th"
    end
  end
end

def parameterize(string, sep = '-')

# => Donald E. Knuth
<%= link_to(@person.name, person_path(@person)) %>

# => #
@person = Person.find(1)

end
end
"#{id}-#{name.parameterize}"
def to_param
class Person

==== Examples

Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
def parameterize(string, sep = '-')
  # replace accented chars with their ascii equivalents
  parameterized_string = transliterate(string)
  # Turn unwanted chars into the separator
  parameterized_string.gsub!(/[^a-z0-9\-_]+/i, sep)
  unless sep.nil? || sep.empty?
    re_sep = Regexp.escape(sep)
    # No more than one of the separator in a row.
    parameterized_string.gsub!(/#{re_sep}{2,}/, sep)
    # Remove leading/trailing separator.
    parameterized_string.gsub!(/^#{re_sep}|#{re_sep}$/i, '')
  end
  parameterized_string.downcase
end

def pluralize(word)

"CamelOctopus".pluralize # => "CamelOctopi"
"words".pluralize # => "words"
"sheep".pluralize # => "sheep"
"octopus".pluralize # => "octopi"
"post".pluralize # => "posts"
Examples:

Returns the plural form of the word in the string.
def pluralize(word)
  apply_inflections(word, inflections.plurals)
end

def safe_constantize(camel_cased_word)


"UnknownModule::Foo::Bar".safe_constantize # => nil
"UnknownModule".safe_constantize # => nil
"blargle".safe_constantize # => nil

unknown.
nil is returned when the name is not in CamelCase or the constant (or part of it) is

end
"C".safe_constantize # => 'outside', same as ::C
C # => 'inside'
C = 'inside'
module M
C = 'outside'

it starts with "::" or not. No lexical context is taken into account:
The name is assumed to be the one of a top-level constant, no matter whether

"Test::Unit".safe_constantize # => Test::Unit
"Module".safe_constantize # => Module

Tries to find a constant with the name specified in the argument string:
def safe_constantize(camel_cased_word)
  begin
    constantize(camel_cased_word)
  rescue NameError => e
    raise unless e.message =~ /(uninitialized constant|wrong constant name) #{const_regexp(camel_cased_word)}$/ ||
      e.name.to_s == camel_cased_word.to_s
  rescue ArgumentError => e
    raise unless e.message =~ /not missing constant #{const_regexp(camel_cased_word)}\!$/
  end
end

def singularize(word)

"CamelOctopi".singularize # => "CamelOctopus"
"word".singularize # => "word"
"sheep".singularize # => "sheep"
"octopi".singularize # => "octopus"
"posts".singularize # => "post"
Examples:

The reverse of +pluralize+, returns the singular form of a word in a string.
def singularize(word)
  apply_inflections(word, inflections.singulars)
end

def tableize(class_name)

"fancyCategory".tableize # => "fancy_categories"
"egg_and_ham".tableize # => "egg_and_hams"
"RawScaledScorer".tableize # => "raw_scaled_scorers"
Examples

uses the +pluralize+ method on the last word in the string.
Create the name of a table like Rails does for models to table names. This method
def tableize(class_name)
  pluralize(underscore(class_name))
end

def titleize(word)

"raiders_of_the_lost_ark".titleize # => "Raiders Of The Lost Ark"
"TheManWithoutAPast".titleize # => "The Man Without A Past"
"x-men: the last stand".titleize # => "X Men: The Last Stand"
"man from the boondocks".titleize # => "Man From The Boondocks"
Examples:

+titleize+ is also aliased as 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(word)
  humanize(underscore(word)).gsub(/\b('?[a-z])/) { $1.capitalize }
end

def transliterate(string, replacement = "?")

# => "Juergen"
transliterate("Jürgen")
I18n.locale = :de

# => "Jurgen"
transliterate("Jürgen")
I18n.locale = :en

Now you can have different transliterations for each locale:

})
}
:rule => lambda {|string| MyTransliterator.transliterate(string)}
:transliterate => {
I18n.backend.store_translations(:de, :i18n => {

requirements, a Proc:
characters to ASCII approximations as shown above, or, for more complex
The value for i18n.transliterate.rule can be a simple Hash that maps

})
}
}
"ö" => "oe"
"ü" => "ue",
:rule => {
:transliterate => {
I18n.backend.store_translations(:de, :i18n => {
# Or set them using Ruby

ö: "oe"
ü: "ue"
rule:
transliterate:
i18n:
# Store the transliterations in locales/de.yml

them as the i18n.transliterate.rule i18n key:
In order to make your custom transliterations available, you must set

to ASCII.
and "ö" to "ue" and "oe", or to add support for transliterating Russian
locale. This can be useful, for example, to transliterate German's "ü"
This method is I18n aware, so you can set up custom approximations for a

e.g, "ø", "ñ", "é", "ß", etc.
Default approximations are provided for Western/Latin characters,

# => "AEroskobing"
transliterate("Ærøskøbing")

exists, a replacement character which defaults to "?".
Replaces non-ASCII characters with an ASCII approximation, or if none
def transliterate(string, replacement = "?")
  I18n.transliterate(ActiveSupport::Multibyte::Unicode.normalize(
    ActiveSupport::Multibyte::Unicode.tidy_bytes(string), :c),
      :replacement => replacement)
end

def underscore(camel_cased_word)

"SSLError".underscore.camelize # => "SslError"

though there are cases where that does not hold:
As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,

"ActiveModel::Errors".underscore # => "active_model/errors"
"ActiveModel".underscore # => "active_model"
Examples:

Changes '::' to '/' to convert namespaces to paths.

Makes an underscored, lowercase form from the expression in the string.
def underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end