Ever questioned how to easily prepend "a" or "an" to a word? Here is how I do it:

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

If you put this baby in some file inside config/initializers of your app, you will be able to do this:

=> "a carrot"
=> "an apple"

Yeah I know, it's awesome! I've just thought every instance of class "String" how to do something new. This is called metaprogramming and this is one of the coolest things I've found out about Ruby. However, don't use my solution if you have more complex requirements, like support for acronyms and abbreviations.

Written by

Hrvoje Šimić