Module: ActiveSupport::CoreExtensions::String::Inflections

Included in:
String
Defined in:
lib/ext/active_support_subset.rb

Overview

String inflections define new methods on the String class to transform names for different purposes. For instance, you can figure out the name of a database from the name of a class. "ScaleScore".tableize => "scale_scores"

Instance Method Summary collapse

Instance Method Details

#camelize(first_letter = :upper) ⇒ Object Also known as: camelcase

By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to ":lower" then camelize produces lowerCamelCase.

camelize will also convert '/' to '::' which is useful for converting paths to namespaces

Examples "active_record".camelize #=> "ActiveRecord" "active_record".camelize(:lower) #=> "activeRecord" "active_record/errors".camelize #=> "ActiveRecord::Errors" "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"



84
85
86
87
88
89
# File 'lib/ext/active_support_subset.rb', line 84

def camelize(first_letter = :upper)
  case first_letter
  when :upper then Inflector.camelize(self, true)
  when :lower then Inflector.camelize(self, false)
  end
end

#constantizeObject

Create a class name from a table name like Rails does for table names to models. Note that this returns a string and not a Class. (To convert to an actual class follow classify with constantize.)

Examples "egg_and_hams".classify #=> "EggAndHam" "post".classify #=> "Post"



111
112
113
# File 'lib/ext/active_support_subset.rb', line 111

def constantize
  Inflector.constantize(self)
end

#underscoreObject

The reverse of camelize. Makes an underscored form from the expression in the string.

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

Examples "ActiveRecord".underscore #=> "active_record" "ActiveRecord::Errors".underscore #=> active_record/errors



100
101
102
# File 'lib/ext/active_support_subset.rb', line 100

def underscore 
  Inflector.underscore(self)
end