[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What is Ruby for "pimpl"?

John Carter

11/29/2004 4:13:00 AM

5 Answers

Jim Weirich

11/29/2004 4:43:00 AM

0

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/go...
>
> 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://onest...
-----------------------------------------------------------------
"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)


Jamis Buck

11/29/2004 4:44:00 AM

0

John Carter wrote:
> A useful dependency cutting tool in C++ is the pimpl idiom.
> http://www.gotw.ca/go...
>
> I trying to imagine a ruby translation of the idiom.
>
> Dependency Injection?

Well, there isn't really a "compile" phase for Ruby, so changing any
interface of any class never requires a recompile. As long as the change
only affects non-published interfaces of the class, the clients of the
class will continue unaffected, blissfully ignorant of your refactoring.

Dependency injection solves a different set of problems than the pimpl
idiom (which is really only necessary for C++).

Does that come close to answering your question? Let me know if I've got
wide of the mark.

- Jamis

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck...


Dave Thomas

11/29/2004 4:57:00 AM

0


On Nov 28, 2004, at 22:13, John Carter wrote:

> A useful dependency cutting tool in C++ is the pimpl idiom.
> http://www.gotw.ca/go...
>
> I trying to imagine a ruby translation of the idiom.

What problem is this trying to solve? (Or, to put it another way, does
the problem arise in the Ruby world?)


Cheers

Dave



John Carter

11/29/2004 4:59:00 AM

0

Jim Weirich

11/29/2004 5:23:00 AM

0

On Sunday 28 November 2004 11:59 pm, John Carter wrote:
First of all ...
> Sorry for thinking out loud, excuse me for existing.

No need to apologize. Just didn't see where you were going with this one.

> On Mon, 29 Nov 2004, John Carter wrote:
> Looking at the error message I realized that class A used B which used C
> which used D which under certain error conditions that could never occur
> while testing A, used E.
>
> This left me feeling my program was too coupled. Why did the syntax of a
> module that I wasn't using block me?

The simplest thing to do would just move the require so that it executes only
when it is really needed.

> -------file A.rb------------------------------------------------------
> class A
> def foo
> dostuff()
> rescue BadThingsHappening => details
require 'FixMe' # Move the require to here
> b = FixMe.new( details, x, y, z)
> b.make_it_all_better
> end
>
> def bah
> end
> end
> ----------------------------------------------------------------------

--
-- Jim Weirich jim@weirichhouse.org http://onest...
-----------------------------------------------------------------
"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)