Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/autumn/misc.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#pluralize(singular, plural = nil) ⇒ Object

Possibly pluralizes a noun based on this number’s value. Returns this number and the noun as a string. This method attempts to use the Ruby English gem if available, and falls back on the very simple default of appending an “s” to the word to make it plural. If the Ruby English gem is not available, you can specify a custom plural form for the word. Examples:

5.pluralize('dog') #=> "5 dogs"
1.pluralize('car') #=> "1 car"
7.pluralize('mouse', 'mice') #=> "7 mice" (only necessary if Ruby English is not installed)


19
20
21
22
23
24
25
26
# File 'lib/autumn/misc.rb', line 19

def pluralize(singular, plural=nil)
  begin
    return "#{to_s} #{self == 1 ? singular : singular.plural}"
  rescue Gem::LoadError
    plural ||= singular + 's'
    return "#{to_s} #{(self == 1) ? singular : plural}"
  end
end