A/An 篇文章 Ruby

A/An articles in Ruby

对于 a/an 篇文章,是否有类似 [#pluralize in ActiveSupport][1] 的内容?

所以基本上我需要这样的东西:

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

您可以将 String#articleize 定义为 return 适当的 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"

看来 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"