无法使用ActiveSupport :: Inflector进行复数化/单一化(在irb中)

时间:2011-02-28 10:25:55

标签: ruby activesupport

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"

嗯,基本上,这就是问题所在。令我感到困惑的是,titleize等方法似乎工作得很好,但tableizepluralizesingularize却没有。

我忘记了要求吗?

(另行注意,我注意到this page提供了"post".pluralize等示例,当我尝试时,会产生NoMethodError: undefined method 'pluralize' for "post":String。但也许这可以为另一个问题保存。)< / p>

1 个答案:

答案 0 :(得分:37)

访问#pluralize而不向String类添加新方法:

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

String课程:

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

实际上调用了下面的ActiveSupport::Inflector.pluralize

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