【问题标题】:A/An articles in Ruby [duplicate]Ruby 中的 A/An 文章 [重复]
【发布时间】:2021-02-10 10:22:59
【问题描述】:

除了a/an 文章之外,是否有类似 [#pluralize in ActiveSupport][1] 的内容?

所以基本上我需要类似的东西:

'status'.articleize # => "a status"
'urgent'.articleize # => "an urgent"

【问题讨论】:

  • 将此添加到您的测试用例中:“他是一名独眼海员,认为在海盗船上服役是一种荣幸”。

标签: ruby activesupport


【解决方案1】:

看来Linguistics gem 可能适合这份工作。让我们试试 Cary Swoveland 的挑战:

require 'linguistics/en'
Linguistics.use(:en)

"one-eyed seaman".en.a
=> "a one-eyed seaman"
"honor".en.a
=> "an honor"

# And OP examples...
"urgent update".en.a
=> "an urgent update"
"status update".en.a
=> "a status update"

【讨论】:

    【解决方案2】:

    您可以定义String#articleize 以返回适当的English article

    class String
      def articleize
        if self[0] =~ /[aeiou]/i
          "an #{self}"
        else
          "a #{self}"
        end
      end
    end
    
    'status'.articleize # => "a status"
    'urgent'.articleize # => "an urgent"
    

    【讨论】:

    • 我在寻找以前的答案时显然没有使用正确的搜索词,因为它现在已被标记为删除。
    猜你喜欢
    • 1970-01-01
    • 2011-07-19
    • 1970-01-01
    • 2023-04-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多