Singularizing and Pluralizing Words

Two ruby facets that can improve your applications "intelligence" with very simple methods (especially on social-networks) are singular and pluralize. You can easily add them as methods accessable all over your application by using application.rb. Here they are:

singular()

Convert an English word from plurel to singular.

  "boys".singular      #=> boy
  "tomatoes".singular  #=> tomato

This method is also aliased as singularize

# File lib/facets/core/string/singular.rb, line 92
  def singular
    result = self.dup
    self.class.inflection_rules_by_plural.each do |(match, replacement)|
      break if result.gsub!(match, replacement)
    end
    return result
  end

# and now to plural
plural()

Convert an English word from singular to plurel.

  "boy".plural     #=> boys
  "tomato".plural  #=> tomatoes

This method is also aliased as pluralize

# File lib/facets/core/string/singular.rb, line 106
  def plural
    return self.dup if /s$/ =~ self
    result = self.dup
    self.class.inflection_rules_by_singular.each do |(match, replacement)|
      break if result.gsub!(match, replacement)
    end
    return result
  end