On Sunday 28 November 2004 11:13 pm, John Carter wrote:
> A useful dependency cutting tool in C++ is the pimpl idiom.
>    http://www.gotw.ca/gotw/024.htm
>
> I trying to imagine a ruby translation of the idiom.

Wow, this takes me back.   I haven't done C++ code in quite some time.  
Hmmm ... a ruby version of pimpl?  There is the obvious hack where you could 
move the entire implementation to a separate object and just delegate/forward 
method calls to the implementation (and using method_missing tricks can make 
this much easier than the C++ version).

But that begs the question of /why/ we should use the pimpl idiom.  To quote 
from the GotW page:  "In C++, when anything in a class definition changes 
(even private members) all users of that class must be recompiled. To reduce 
these dependencies, a common technique is to use an opaque pointer to hide 
some of the implementation details."

There's the nub of the matter.  In Ruby, nothing needs recompiled if a class 
changes.  Using pimpl in Ruby has all the disadvantages of the C++ version 
(indirection, extra objects) and none of the benefits. 

> Dependency Injection?

Actually, I see DI as more closely related to the /other/ compile dependency 
reduction technique often used in C++: Abstract Base Classes (which I 
generally used instead of the pimpl idiom).  And again, in Ruby, through the 
"magic" of duck typing, the ABC idiom is trivial.

-- 
-- Jim Weirich    jim / weirichhouse.org     http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct, 
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)