module Pluralizr
A module that takes a single word and returns the pluralized version of that word. It attempts to take into account various rules—like, handling words that end in ‘y’ where the ‘y’ is preceded by a vowel or consonant (i.e., day
-> days
versus baby
-> babies
)—as well as words of French, Greek and Latin origins.
Constants
- EXCEPTIONS_LIST
List of words that are exceptions to the rules in the
RULES
constant variable hash.- IRREGULAR_WORDS
List of words whose plural form doesn’t adhere to a rule in the
RULES
hash, but instead, often mutates the original word. For example,mouse
=>mice
. Or,foot
=>feet
.- PLURAL_ONLY_WORDS
List of words that only have a plural form. There is no singular form of the word. For example, the word
tweezers
has no singular form. The word tweezer is actually a verb and not a noun.- RULES
Hash containing the various rules for specific word endings in the English language.
- SINGULAR_AND_PLURAL_FORMS_ARE_THE_SAME
List of words whose plural form is the same as its singular form. For example,
deer
is used in both singular and plural contexts.- VERSION
Current gem version.
Public Class Methods
Pluralizes the object passed in or returns an error if an exception is raised.
Parameters:¶ ↑
- word
-
The object passed in.
Returns:¶ ↑
Either an error message, if any exceptions are raised via Pluralizr.check_for_errors
method, or the appropriate plural version of the word passed into the method.
# File lib/pluralizr.rb, line 25 def self.pluralize(word) check_for_errors(word) case when word.is_irregular? then return IRREGULAR_WORDS[word] when word.has_same_singular_and_plural_form? || word.only_has_plural_form? return word when word.ends_with_vowel_and_y_or_o? || word.ends_with_double_vowel_and_f? return word.insert(-1, 's') else RULES.each do |rule, props| return find_plural_of(word, props) if word.end_with?(rule) end end return find_plural_of(word, RULES['default']) end
Protected Class Methods
Checks if word passed into the Pluralizr.pluralize
method raises any errors. Currently, the following errors are checked: Pluralizr::InvalidTypeError
, Pluralizr::InvalidStringError
, and Pluralizr::TooManyWordsError
.
Parameters:¶ ↑
- word
-
A
String
representing the original word passed in.
Returns:¶ ↑
An error message and backtrace if an error is raised.
# File lib/pluralizr.rb, line 115 def self.check_for_errors(word) raise InvalidTypeError if !word.is_a? String raise InvalidStringError if word.match(/^[\d\W_]/) raise TooManyWordsError if !word.match(/^[a-z]+$/i) end
Converts the word passed into the Pluralizr.pluralize
method into its plural form.
Parameters:¶ ↑
- word
-
A
String
representing the original word passed in. - with_ending
-
A
String
representing the word’s plural form ending. - offset
-
An Integer representing the number of letters to remove from the end of the original word, before appending the plural form ending.
Returns:¶ ↑
A string representing the pluralized form of the original word passed in.
# File lib/pluralizr.rb, line 136 def self.convert_ending_of(word, with_ending, offset) word[0..offset].insert(-1, with_ending) end
Determines which implementation method to use based on several factors: whether or not the rule from the RULES
hash matching the word being passed in has an exceptions list and, if so, whether the word is on that list or not.
Parameters:¶ ↑
- word
-
A
String
representing the original word passed in. - props
-
A hash value representing the properties of the rule,
from the RULES
hash, matching the word passed in.
Returns:¶ ↑
The pluralized form of the original word passed in.
# File lib/pluralizr.rb, line 94 def self.find_plural_of(word, props) if !props[:has_exception_list] convert_ending_of(word, props[:with_ending], props[:offset]) elsif EXCEPTIONS_LIST.include?(word) EXCEPTIONS_LIST[word] else convert_ending_of(word, props[:with_ending], props[:offset]) end end
Public Instance Methods
Returns true
if the word (the caller) ends in a double vowel plus ‘f’. For example, proof
or handkerchief
.
# File lib/pluralizr.rb, line 54 def ends_with_double_vowel_and_f? self.match(/[aeiouy]+[f]$/i) end
Returns true
if the word (the caller) ends in a vowel plus either a ‘y’ or an ‘o’. For example, day
and valley
or stereo
and tattoo
.
# File lib/pluralizr.rb, line 47 def ends_with_vowel_and_y_or_o? self.match(/[aeiouy][oy]$/i) end
Returns true
if the word (the caller) is found in the SINGULAR_AND_PLURAL_FORMS_ARE_THE_SAME
hash.
# File lib/pluralizr.rb, line 75 def has_same_singular_and_plural_form? SINGULAR_AND_PLURAL_FORMS_ARE_THE_SAME.include?(self) end
Returns true
if the word (the caller) is found in the IRREGULAR_WORDS
hash.
# File lib/pluralizr.rb, line 61 def is_irregular? IRREGULAR_WORDS.include?(self) end
Returns true
if the word (the caller) is found in the PLURAL_ONLY_WORDS
hash.
# File lib/pluralizr.rb, line 68 def only_has_plural_form? PLURAL_ONLY_WORDS.include?(self) end