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
。但是也许这可以保存另一个问题。) 最佳答案
无需添加新方法到#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
关于ruby - 使用ActiveSupport::Inflector(在irb中)无法获得复数/单数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5140753/