Search code examples
ruby-on-rails-3pluralizesingular

rails singularize to a / an


Is there a method that works similar to singularize to prepend "a" or "an according to the word?

  • like f(apple) # => an apple
  • f(carpet) #=> a carpet

Solution

  • Look here http://deveiate.org/projects/Linguistics/wiki/English and check out this question

    If you need something simpler, something that will for instance prepend "an" if a word starts with vowel, you can use my one liner:

    String.class_eval { def prepend; %w(a e i o u).include?(downcase.first) ? "an #{self}" : "a #{self}"; end }
    

    Put this in a file prepend.rb in config/initializers folder of your application.

    Then you will be able to use

    "carrot".prepend => "a carrot"
    "apple".prepend => "an apple"