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}"
end
end
If you put this baby in some file inside config/initializers of your app, you will be able to do this:
"carrot".a_or_an
=> "a carrot"
"apple".a_or_an
=> "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.