# Interface coding by contract design and dependency injection

**URL:** <https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795>\
**Category:** Ruby\
**Created:** [January 29, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795 "2007-01-29T19:46:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [January 29, 2007, 7:46pm UTC](https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795/1 "2007-01-29T19:46:12Z")

</div>

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  
written  
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?  
I  
was wondering whether to create Modules in place of my interfaces &  
provide  
them with methods which throw Exceptions, thus forcing them to be  
overrriden  
in the classes that include them? Also how does one manage dependency  
injection in Ruby? Is there a good framework/library/pattern typically  
used  
for this, or are there good ‘Ruby reasons’ not to use DI?

Many thanks

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [January 29, 2007, 8:10pm UTC](https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795/2 "2007-01-29T20:10:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [January 29, 2007, 9:28pm UTC](https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795/3 "2007-01-29T21:28:35Z")

</div>

SonOfLilit wrote:

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

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

> **[Dependency Injection](https://de.wikipedia.org/wiki/Dependency_Injection)**
>
> Als Dependency Injection (DI, englisch dependency ‚Abhängigkeit‘ und injection ‚Injektion‘, deutsch Abhängigkeitsinjektion oder Einbringen von Abhängigkeiten) wird in der objektorientierten Programmierung ein Entwurfsmuster bezeichnet, welches die Abhängigkeiten eines Objekts zur Laufzeit reglementiert: Benötigt ein Objekt beispielsweise bei seiner Initialisierung ein anderes Objekt, ist diese Abhängigkeit an einem zentralen Ort hinterlegt – es wird also nicht vom initialisierten Objekt selbst Di...

- Phill

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [January 29, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795/4 "2007-01-29T23:13:58Z")

</div>

Did you mean [http://onestepback.org/index.cgi/Tech/Ruby/](http://onestepback.org/index.cgi/Tech/Ruby/)  
DependencyInjectionInRuby.rdoc ?

Paulo Jorge Duarte  
Köremoved\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [January 30, 2007, 12:24am UTC](https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795/5 "2007-01-30T00:24:11Z")

</div>

Hi,

I’m really new to dependency injection in general, but I hope I can  
help,  
though. I am aware of two libraries for what you want to do. Copland,  
first,  
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  
is  
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  
know  
much about it, except it supports a Service Locator mechanism.

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

[http://needle.rubyforge.org/](http://needle.rubyforge.org/)  
[http://copland.rubyforge.org/](http://copland.rubyforge.org/)

–  
Olivier R.

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

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [January 30, 2007, 9:38pm UTC](https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795/6 "2007-01-30T21:38:53Z")

</div>

Paulo Köch wrote:

> Did you mean  
> [http://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc](http://onestepback.org/index.cgi/Tech/Ruby/DependencyInjectionInRuby.rdoc) ?
> 
> Paulo Jorge Duarte Köch  
> removed\_email\_address@domain.invalid

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

---

<div class="post-metadata">

**Author:** ![PerfectDayToChaseTo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/898d66/32.png) [@PerfectDayToChaseTo](https://www.ruby-forum.com/u/PerfectDayToChaseTo)\
**Post date:** [January 30, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/interface-coding-by-contract-design-and-dependency-injection/83795/7 "2007-01-30T20:55:48Z")

</div>

Thanks Guys,

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