A/An articles in Ruby

Viewed 95

Is there something like [#pluralize in ActiveSupport][1] but for a/an articles?

So basically I need something like:

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

It seems the Linguistics gem might be up for the job. Let's try Cary Swoveland's challenge:

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"

You could define String#articleize to return the appropriate 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"
Related