In studying mixins vs. dependency injection, I often hear the phrase "the Ruby way." Often developers say something along the lines of
Ruby lets you reopen classes and redefine methods means that you can easily "inject" new references into your code at test-time.
(see #6 at http://weblog.jamisbuck.org/2007/7/29/net-ssh-revisited)
But testing is not my main concern; my concern is class reuse. I want classes I can reuse in multiple enterprise-scale Rails applications.
So what happened to REUSING classes? Using mixins and reopening classes does not seem to provide a way to write classes in such a way that they are decoupled from application-specific details without a bunch of extra work. But Perhaps I am wrong. If I am, can someone provide a link to an article containing sample code that clearly explains how to accomplish this properly using mixins and reopening of classes?
As an example, the class Foo here is coupled to the class Logger:
class Foo
def initialize
@logger = new_logger
end
def new_logger
Logger.new
end
end
Yes, I can reopen Foo and redefine new_logger, but I just cannot believe this is considered a realistic, standard approach to writing reusable classes usable by multiple Rails applications.