cheat sheets.

$ cheat ruby_strings
camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true):
"active_record".camelize                # => "ActiveRecord"
"active_record".camelize(:lower)        # => "activeRecord"
"active_record/errors".camelize         # => "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) # => "activeRecord::Errors"
"SSLError".underscore.camelize          # => "SslError"


classify(table_name):
"egg_and_hams".classify # => "EggAndHam"
"posts".classify        # => "Post"
"business".classify     # => "Busines"


constantize(camel_cased_word):
"Module".constantize     # => Module
"Test::Unit".constantize # => Test::Unit


dasherize(underscored_word):
"puni_puni" # => "puni-puni"


demodulize(class_name_in_module):
"ActiveRecord::CoreExtensions::String::Inflections".demodulize # =>
"Inflections"
"Inflections".demodulize                                       # =>
"Inflections"


foreign_key(class_name, separate_class_name_and_id_with_underscore = true):
"Message".foreign_key        # => "message_id"
"Message".foreign_key(false) # => "messageid"
"Admin::Post".foreign_key    # => "post_id"


humanize(lower_case_and_underscored_word):
"employee_salary" # => "Employee salary"
"author_id"       # => "Author"


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


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


parameterize(string, sep = '-'):
"Donald E. Knuth".parameterize # => "donald-e-knuth"


pluralize(word):
"post".pluralize             # => "posts"
"octopus".pluralize          # => "octopi"
"sheep".pluralize            # => "sheep"
"words".pluralize            # => "words"
"CamelOctopus".pluralize     # => "CamelOctopi"


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


tableize(class_name):
"RawScaledScorer".tableize # => "raw_scaled_scorers"
"egg_and_ham".tableize     # => "egg_and_hams"
"fancyCategory".tableize   # => "fancy_categories"


titleize(word):
"man from the boondocks".titleize # => "Man From The Boondocks"
"x-men: the last stand".titleize  # => "X Men: The Last Stand"


transliterate(string, replacement = "?"):
nsliterate("Ærøskøbing") # => "AEroskobing"


underscore(camel_cased_word):
"ActiveRecord".underscore         # => "active_record"
"ActiveRecord::Errors".underscore # => active_record/errors
"SSLError".underscore.camelize    # => "SslError"
Version 1, updated 189 days ago.
. o 0 ( edit | history )
( add new | see all )