Ruby 中没有复数函数吗?Rails 中没有吗?

发布于 2024-08-25 22:36:58 字数 428 浏览 8 评论 0原文

我正在编写一些 Ruby 代码,而不是 Rails,我需要处理这样的事情:

found 1 match
found 2 matches

我安装了 Rails,所以也许我可以在脚本顶部添加一个 require 子句,但确实如此有人知道可以复数字符串的 RUBY 方法吗?如果脚本不是 Rails 但我安装了 Rails,是否有一个我可以要求的类可以处理这个问题?

编辑:所有这些答案都很接近,但我勾选了对我有用的答案。 在编写 Ruby(而不是 Rails)代码时尝试使用此方法作为帮助器:

def pluralize(number, text)
  return text.pluralize if number != 1
  text
end

I am writing some Ruby code, not Rails, and I need to handle something like this:

found 1 match
found 2 matches

I have Rails installed so maybe I might be able to add a require clause at the top of the script, but does anyone know of a RUBY method that pluralizes strings? Is there a class I can require that can deal with this if the script isn't Rails but I have Rails installed?

Edit: All of these answers were close but I checked off the one that got it working for me.
Try this method as a helper when writing Ruby, not Rails, code:

def pluralize(number, text)
  return text.pluralize if number != 1
  text
end

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

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

发布评论

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

评论(7

我纯我任性 2024-09-01 22:36:58

实际上,您需要做的就是

require 'active_support/inflector'

扩展 String 类型。

然后你可以执行

"MyString".pluralize

以下操作,这将返回

"MyStrings"

2.3.5 try:

require 'rubygems'
require 'active_support/inflector'

应该得到它,如果没有尝试

sudo gem install activesupport

,则需要。

Actually all you need to do is

require 'active_support/inflector'

and that will extend the String type.

you can then do

"MyString".pluralize

which will return

"MyStrings"

for 2.3.5 try:

require 'rubygems'
require 'active_support/inflector'

should get it, if not try

sudo gem install activesupport

and then the requires.

江南烟雨〆相思醉 2024-09-01 22:36:58

Inflector 在大多数情况下都太过分了。

def x(n, singular, plural=nil)
    if n == 1
        "1 #{singular}"
    elsif plural
        "#{n} #{plural}"
    else
        "#{n} #{singular}s"
    end
end

把它放在 common.rb 中,或者任何你喜欢通用实用函数的地方......

require "common" 

puts x(0, 'result') # 0 results
puts x(1, 'result') # 1 result
puts x(2, 'result') # 2 results

puts x(0, 'match', 'matches') # 0 matches
puts x(1, 'match', 'matches') # 1 match 
puts x(2, 'match', 'matches') # 2 matches 

Inflector is overkill for most situations.

def x(n, singular, plural=nil)
    if n == 1
        "1 #{singular}"
    elsif plural
        "#{n} #{plural}"
    else
        "#{n} #{singular}s"
    end
end

Put this in common.rb, or wherever you like your general utility functions and...

require "common" 

puts x(0, 'result') # 0 results
puts x(1, 'result') # 1 result
puts x(2, 'result') # 2 results

puts x(0, 'match', 'matches') # 0 matches
puts x(1, 'match', 'matches') # 1 match 
puts x(2, 'match', 'matches') # 2 matches 
念﹏祤嫣 2024-09-01 22:36:58

我个人喜欢绝对与 Rails 无关的 语言学宝石

# from it's frontpage
require 'linguistics'

Linguistics.use :en

"box".en.plural #=> "boxes"
"mouse".en.plural #=> "mice"
# etc

I personally like the linguistics gem that is definitely not rails related.

# from it's frontpage
require 'linguistics'

Linguistics.use :en

"box".en.plural #=> "boxes"
"mouse".en.plural #=> "mice"
# etc
咽泪装欢 2024-09-01 22:36:58

这对我有用(使用 ruby​​ 2.1.1 和 actionpack 3.2.17):

~$ irb
>> require 'action_view'
=> true
>> include ActionView::Helpers::TextHelper
=> Object
>> pluralize(1, 'cat')
=> "1 cat"
>> pluralize(2, 'cat')
=> "2 cats"

This works for me (using ruby 2.1.1 and actionpack 3.2.17):

~$ irb
>> require 'action_view'
=> true
>> include ActionView::Helpers::TextHelper
=> Object
>> pluralize(1, 'cat')
=> "1 cat"
>> pluralize(2, 'cat')
=> "2 cats"
对风讲故事 2024-09-01 22:36:58
require 'active_support'
require 'active_support/inflector'

inf = ActiveSupport::Inflector::Inflections.new

得到偏转器,不知道你如何使用它

require 'active_support'
require 'active_support/inflector'

inf = ActiveSupport::Inflector::Inflections.new

to get the inflector, not sure how you use it

王权女流氓 2024-09-01 22:36:58

我的解决方案:

# Custom pluralize - will return text without the number as the default pluralize.
def cpluralize(number, text)
  return text.pluralize if number != 1 
  return text.singularize if number == 1
end

因此,如果您调用 cpluralize(1, 'reviews') ,您可以返回“评论”

希望有所帮助。

my solution:

# Custom pluralize - will return text without the number as the default pluralize.
def cpluralize(number, text)
  return text.pluralize if number != 1 
  return text.singularize if number == 1
end

So you can have 'review' returned if you call cpluralize(1, 'reviews')

Hope that helps.

淡淡绿茶香 2024-09-01 22:36:58

我为此定义了一个辅助函数,我将它用于每个用户可编辑模型的索引视图:

  def ovyka_counter(array, name=nil, plural=nil)
    name ||= array.first.class.human_name.downcase
    pluralize(array.count, name, plural)
  end

然后您可以从视图中调用它:

<% ovyka_counter @posts %>

对于国际化(i18n),您可以将其添加到您的语言环境 YAML 文件中:

  activerecord:
    models:
      post: "Conversation"

I've defined a helper function for that, I use it for every user editable model's index view :

  def ovyka_counter(array, name=nil, plural=nil)
    name ||= array.first.class.human_name.downcase
    pluralize(array.count, name, plural)
  end

then you can call it from the view :

<% ovyka_counter @posts %>

for internationalization (i18n), you may then add this to your locale YAML files :

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