I’m creating a gem that adds a new method to the String class. For instance if I were creating a method that added the to_foobar on String I know that I can simply do:
class String
def to_foobar
# ...
end
end
But is this the right way to do this or is there a better way?
I would prefer
That way, if I get for example an error message mentioning
String#to_foobar, I don’t have to wonder “Wait,Stringdoesn’t have ato_foobarmethod, where is that coming from?” I can just sayEt voilà, there’s this suspicious looking module in the ancestry chain, which, following standard Ruby naming conventions, probably lives in
my_gem/lib/my_gem/core_extensions/string.rb.Or, I can say
And actually get a meaningful answer.
Of course,
always works.