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

发布于 2024-10-19 22:56:30 字数 932 浏览 0 评论 0原文

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 却不起作用。

我忘记要求什么了吗?

(另外,我注意到此页面提供了诸如“post”.pluralize<之类的示例/code>,当我尝试时,导致 NoMethodError: undefined method 'pluralize' for "post":String 但也许这是另一个问题。)

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.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许仙没带伞 2024-10-26 22:56:30

无需向 String 类添加新方法即可访问 #pluralize

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文