Search code examples
rubyactivesupport

Can't get pluralize/singularize working with ActiveSupport::Inflector (in irb)


irb(main):001:0> require 'active_support'
=> true
irb(main):002:0> require 'active_support/inflector/inflections'
=> true
irb(main):003:0> ActiveSupport::Inflector.pluralize('test')
=> "test"
irb(main):004:0> ActiveSupport::Inflector.singularize('tests')
=> "tests"
irb(main):005:0> ActiveSupport::Inflector.titleize('hat simulator')
=> "Hat Simulator"
<ort::Inflector.tableize("america's number one hat simulator")
=> "america's number one hat simulator"

Well, basically, that's the question. It's confusing me that methods such as titleize seem to work fine, but tableize, pluralize and singularize don't.

Have I forgotten to require something?

(On a separate note, I notice this page provides examples like "post".pluralize, which when I tried, resulted in NoMethodError: undefined method 'pluralize' for "post":String. But maybe that's something to save for another question.)


Solution

  • Access to #pluralize without adding new methods to the String class:

    require 'active_support/inflector'
    ActiveSupport::Inflector.pluralize('test')
    #=> "tests"
    

    For String class:

    require 'active_support/core_ext/string'
    "test".pluralize
    #=> "tests"
    

    which actually calls ActiveSupport::Inflector.pluralize underneath:

    def pluralize
      ActiveSupport::Inflector.pluralize(self)
    end