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
等方法似乎工作得很好,但tableize
,pluralize
和singularize
却没有。
我忘记了要求吗?
(另行注意,我注意到this page提供了"post".pluralize
等示例,当我尝试时,会产生NoMethodError: undefined method 'pluralize' for "post":String
。但也许这可以为另一个问题保存。)< / p>
答案 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