Search code examples
rubyactivesupport

A/An articles in Ruby


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"

Solution

  • 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"