rails singularize to a / an
-
04-03-2021 - |
Question
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"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow