Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rails Inflections

David Celis
September 02, 2012

Rails Inflections

Learn a bit about the Inflector in ActiveSupport, and how much of a mess it is.

Unfortunately, the animation that shows all of the pluralization and singularization rules doesn't appear, but you can view them here: https://gist.github.com/3137677

David Celis

September 02, 2012
Tweet

More Decks by David Celis

Other Decks in Programming

Transcript

  1. handles IZATION TITLE ‘2001: a space odyssey’.titleize # => “2001:

    A Space Odyssey” ActiveSupport::Inflector Saturday, September 1, 12
  2. WHY IS THIS HAPPENING WHY DO BAD THINGS HAPPEN TO

    GOOD PEOPLE Saturday, September 1, 12
  3. SIBILANT SOUNDS Add -es to words ending in a consonant

    sibilant sound (-s, -x, -z, -ch, -sh) precious → preciouses Saturday, September 1, 12
  4. THE OES RULE Add -es if the word ends in

    -o preceded by a consonant tomato → tomatoes Saturday, September 1, 12
  5. Change -y to -ies if the -y is THE IES

    RULE berry → berries Saturday, September 1, 12
  6. Change -y to -ies if the -y is preceded by

    a consonant THE IES RULE berry → berries Saturday, September 1, 12
  7. require 'active_support/inflector/inflections' module ActiveSupport Inflector.inflections do |inflect| inflect.plural(/$/, 's') inflect.plural(/([sxz]|[cs]h)$/i,

    '\1es') inflect.plural(/([^aeiouy]o)$/i, '\1es') inflect.plural(/([^aeiouy])y$/i, '\1ies') inflect.singular(/s$/i, '') inflect.singular(/(ss)$/i, '\1') inflect.singular(/([sxz]|[cs]h)es$/i, '\1') inflect.singular(/([^aeiouy]o)es$/i, '\1') inflect.singular(/([^aeiouy])ies$/i, '\1y') inflect.irregular('person', 'people') inflect.irregular('child', 'children') inflect.irregular('self', 'selves') inflect.uncountable(%w(series)) end end Saturday, September 1, 12
  8. WE GO FROM TO 21 PLURALIZATION RULES 4 27 SINGULARIZATION

    RULES 5 7 IRREGULARITIES 3 10 UNCOUNTABLES 1 TO Saturday, September 1, 12
  9. WE GO FROM TO 21 PLURALIZATION RULES 4 27 SINGULARIZATION

    RULES 5 7 IRREGULARITIES 3 10 UNCOUNTABLES 1 TO Saturday, September 1, 12
  10. WE GO FROM TO 21 PLURALIZATION RULES 4 27 SINGULARIZATION

    RULES 5 7 IRREGULARITIES 3 10 UNCOUNTABLES 1 TO Saturday, September 1, 12
  11. WE GO FROM TO 21 PLURALIZATION RULES 4 27 SINGULARIZATION

    RULES 5 7 IRREGULARITIES 3 10 UNCOUNTABLES 1 TO Saturday, September 1, 12
  12. module ActiveSupport Inflector.inflections(:en) do |inflect| # english rules go here

    end end module ActiveSupport Inflector.inflections(:es) do |inflect| # spanish rules go here end end >> 'person'.pluralize(:en) => “people” >> 'avión'.pluralize(:es) => “aviones” >> 'avión'.pluralize(:en) => “avións” Saturday, September 1, 12