Interface coding by contract design and dependency injection

Hi All,

I am a Java developer of many years & just starting to get into Ruby (&
really enjoying it!). However I am used to ‘coding by contract’ using
interfaces. This is especially great when using dependency injection
(usually via Spring). It makes testing very easy, as ‘mocks’ can be
simply & then plugged in. I know that there isn’t an equvalent for an
interface in Ruby, but is there a pattern which would typically be used?
was wondering whether to create Modules in place of my interfaces &
them with methods which throw Exceptions, thus forcing them to be
in the classes that include them? Also how does one manage dependency
injection in Ruby? Is there a good framework/library/pattern typically
for this, or are there good ‘Ruby reasons’ not to use DI?

Many thanks

Thee third google result for “dependency injection” is ruby-specific.

SonOfLilit wrote:

Thee third google result for “dependency injection” is ruby-specific.

For me, it’s a German Wikipedia article on DI.

  • Phill

Did you mean
DependencyInjectionInRuby.rdoc ?

Paulo Jorge Duarte
[email protected]


I’m really new to dependency injection in general, but I hope I can
though. I am aware of two libraries for what you want to do. Copland,
is an Inversion of Control (IoC) container, based on the Hivemind IoC
container for java.
As you said, there is no interface in Ruby, so Interface based injection
not supported. However, Type 2 IoC (Setter Injection) and type 3 IoC
(Constructor Injection) are supported.
The second is Needle, which is a Dependency Injection container. I don’t
much about it, except it supports a Service Locator mechanism.

By the way, the documentations are great for both ! Read them both and
your choice !

Olivier R.

Le lundi 29 janvier 2007 19:45, PerfectDayToChaseTornados a écrit :

Paulo Köch wrote:

Did you mean ?

Paulo Jorge Duarte Köch
[email protected]

That didn’t show up for me. Google results are depending on which data
center you catch, so just saying “google, and result #X will be
relevant” is a questionable technique to answer a question.

Over by the window lie the raiment and the weapons
That we need to take into this world today
Armoured by opinion, with statistic and schoolboy’s charm
We take our place amongst the rank and file

Skyclad - A Survival Campaign

Thanks Guys,

I’ll check out Copland & Needle, anyone have any experience with either
these with Rails?