This module provides english singular <-> plural noun inflections.
By default, camelize converts strings to UpperCamelCase.
camelize will also convert ’/’ to ’::’ which is useful for converting paths to namespaces
@example
"active_record".camelize #=> "ActiveRecord" "active_record/errors".camelize #=> "ActiveRecord::Errors"
# File lib/extlib/inflection.rb, line 32 32: def camelize(lower_case_and_underscored_word, *args) 33: lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } 34: end
Take an underscored name and make it into a camelized name
@example
"egg_and_hams".classify #=> "EggAndHam" "enlarged_testes".classify #=> "EnlargedTestis" "post".classify #=> "Post"
# File lib/extlib/inflection.rb, line 18 18: def classify(name) 19: words = name.to_s.sub(/.*\./, '').split('_') 20: words[1] = singularize(words[1]) 21: words.collect { |word| word.capitalize }.join 22: end
# File lib/extlib/inflection.rb, line 138 138: def clear(type = :all) 139: if type == :singular || type == :all 140: @singular_of = {} 141: @singular_rules = [] 142: @singularization_rules, @singularization_regex = nil, nil 143: end 144: if type == :plural || type == :all 145: @singular_of = {} 146: @singular_rules = [] 147: @singularization_rules, @singularization_regex = nil, nil 148: end 149: end
Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
@example
"Module".constantize #=> Module "Class".constantize #=> Class
# File lib/extlib/inflection.rb, line 98 98: def constantize(camel_cased_word) 99: unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word 100: raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" 101: end 102: 103: Object.module_eval("::#{$1}", __FILE__, __LINE__) 104: end
Removes the module part from the expression in the string
@example
"ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections" "Inflections".demodulize #=> "Inflections"
# File lib/extlib/inflection.rb, line 64 64: def demodulize(class_name_in_module) 65: class_name_in_module.to_s.gsub(/^.*::/, '') 66: end
Creates a foreign key name from a class name.
@example
"Message".foreign_key #=> "message_id" "Admin::Post".foreign_key #=> "post_id"
# File lib/extlib/inflection.rb, line 87 87: def foreign_key(class_name, key = "id") 88: underscore(demodulize(class_name.to_s)) << "_" << key.to_s 89: end
Capitalizes the first word and turns underscores into spaces and strips _id. Like titleize, this is meant for creating pretty output.
@example
"employee_salary" #=> "Employee salary" "author_id" #=> "Author"
# File lib/extlib/inflection.rb, line 55 55: def humanize(lower_case_and_underscored_word) 56: lower_case_and_underscored_word.to_s.gsub(/_id$/, '').tr('_', ' ').capitalize 57: end
Convert an English word from singular to plural.
"boy".plural #=> boys "tomato".plural #=> tomatoes
word | word to pluralize |
pluralized form of word |
Aliased as pluralize (a Railism)
# File lib/extlib/inflection.rb, line 305 305: def plural(word) 306: # special exceptions 307: return "" if word == "" 308: if result = plural_of[word] 309: return result.dup 310: end 311: result = word.dup 312: regex, hash = pluralization_rules 313: result.sub!(regex) {|m| hash[m]} 314: plural_of[word] = result 315: return result 316: end
Define a plurualization rule.
singular | ending of the word in singular form |
plural | ending of the word in plural form |
Once the following rule is defined: English::Inflect.singular_rule ‘fe’, ‘ves’
You can see the following results: irb> “wife”.plural
# File lib/extlib/inflection.rb, line 237 237: def plural_rule(singular, plural) 238: @plural_rules << [singular, plural] 239: end
Define a pluralization exception.
singular | singular form of the word |
plural | plural form of the word |
# File lib/extlib/inflection.rb, line 171 171: def plural_word(singular, plural) 172: @plural_of[singular] = plural 173: @plural_of[singular.capitalize] = plural.capitalize 174: end
Read prepared pluralization rules.
# File lib/extlib/inflection.rb, line 253 253: def pluralization_rules 254: if defined?(@pluralization_regex) && @pluralization_regex 255: return [@pluralization_regex, @pluralization_hash] 256: end 257: @pluralization_regex = Regexp.new("(" + @plural_rules.map {|s,p| s}.join("|") + ")$", "i") 258: @pluralization_hash = Hash[*@plural_rules.flatten] 259: [@pluralization_regex, @pluralization_hash] 260: end
Define a general rule.
singular | ending of the word in singular form |
plural | ending of the word in plural form |
whole_word | for capitalization, since words can be capitalized (Man => Men) # |
Once the following rule is defined: English::Inflect.rule ‘y’, ‘ies’
You can see the following results: irb> “fly”.plural
irb> “cry”.plural
Define a general rule.
# File lib/extlib/inflection.rb, line 197 197: def rule(singular, plural, whole_word = false) 198: singular_rule(singular, plural) 199: plural_rule(singular, plural) 200: word(singular, plural) if whole_word 201: end
Convert an English word from plural to singular.
"boys".singular #=> boy "tomatoes".singular #=> tomato
word | word to singularize |
singularized form of word |
Aliased as singularize (a Railism)
# File lib/extlib/inflection.rb, line 277 277: def singular(word) 278: if result = singular_of[word] 279: return result.dup 280: end 281: result = word.dup 282: regex, hash = singularization_rules 283: result.sub!(regex) {|m| hash[m]} 284: singular_of[word] = result 285: return result 286: end
Define a singularization rule.
singular | ending of the word in singular form |
plural | ending of the word in plural form |
Once the following rule is defined: English::Inflect.singular_rule ‘o’, ‘oes’
You can see the following results: irb> “heroes”.singular
# File lib/extlib/inflection.rb, line 218 218: def singular_rule(singular, plural) 219: @singular_rules << [singular, plural] 220: end
Define a singularization exception.
singular | singular form of the word |
plural | plural form of the word |
# File lib/extlib/inflection.rb, line 159 159: def singular_word(singular, plural) 160: @singular_of[plural] = singular 161: @singular_of[plural.capitalize] = singular.capitalize 162: end
Read prepared singularization rules.
# File lib/extlib/inflection.rb, line 242 242: def singularization_rules 243: if defined?(@singularization_regex) && @singularization_regex 244: return [@singularization_regex, @singularization_hash] 245: end 246: # No sorting needed: Regexen match on longest string 247: @singularization_regex = Regexp.new("(" + @singular_rules.map {|s,p| p}.join("|") + ")$", "i") 248: @singularization_hash = Hash[*@singular_rules.flatten].invert 249: [@singularization_regex, @singularization_hash] 250: end
Create the name of a table like Rails does for models to table names. This method uses the pluralize method on the last word in the string.
@example
"RawScaledScorer".tableize #=> "raw_scaled_scorers" "EnlargedTestis".tableize #=> "enlarged_testes" "egg_and_ham".tableize #=> "egg_and_hams" "fancyCategory".tableize #=> "fancy_categories"
# File lib/extlib/inflection.rb, line 76 76: def tableize(class_name) 77: words = class_name.to_const_path.tr('/', '_').split('_') 78: words[1] = pluralize(words[1]) 79: words.join('_') 80: end
The reverse of camelize. Makes an underscored form from the expression in the string.
Changes ’::’ to ’/’ to convert namespaces to paths.
@example
"ActiveRecord".underscore #=> "active_record" "ActiveRecord::Errors".underscore #=> active_record/errors
# File lib/extlib/inflection.rb, line 45 45: def underscore(camel_cased_word) 46: camel_cased_word.to_const_path 47: end
Defines a general inflection exception case.
singular | singular form of the word |
plural | plural form of the word |
Here we define erratum/errata exception case:
English::Inflect.word “erratum”, “errata“
In case singular and plural forms are the same omit second argument on call:
English::Inflect.word ‘information’
# File lib/extlib/inflection.rb, line 132 132: def word(singular, plural=nil) 133: plural = singular unless plural 134: singular_word(singular, plural) 135: plural_word(singular, plural) 136: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.