Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm primarily an ASP.NET developer and the single thing i've been introduced to in recent years that has had the most dramatic impact on the quality of the code I write has learning how to use IoC containers to manage dependencies between different layers of the application efficiently.

I've recently made it my hobby to learn Ruby (and Rails) for personal projects, and I love the language so far. However, one of the things i've noticed from the various tutorials and references i've been using, has been that there has been no mention yet of using IoC. This is in stark contrast to .NET where it is (and where it isn't, should be) drummed into developers at an early stage.

I appreciate that one reason why dependency injection may not be necessary in Ruby, is that everything in a class is public and virtual so open to modification so it's not really needed in order to write good unit tests. But there are of course many other benefits to using IoC containers such as the decoupling options this brings, managing object creation and lifetime etc.

Is dependency injection used in Ruby? If not, why is this not an issue?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

Inversion of Control is still a concept that applies well. You don't need to be doing dependency injection to apply IoC, although in .NET they do tend to go together often.

The big drive behind dependency injection in .NET is avoiding depending on concrete implementations. In Ruby, as you mentioned, things are much more open and you can replace implementation of a class at run time, so there's much less need to create "interfaces" and explicitly inject dependencies.

It can be argued that interfaces and IoC/DI aren't strictly necessary in .NET either (even when it comes to testing -- there are mocking frameworks that will allow you to eliminate the need to interface everything under the sun), but it's more pronounced in Ruby.

share|improve this answer
add comment

Dependency Injection is less important in dynamic languages because they are dynamic- you can change your code whenever you want so you don't have to plan ahead for potentially changing dependencies. There are easier ways to achieve IoC in a language like Ruby.

Fabio Kung on this topic which refers to Jamis Buck on the same thing - those give a bit of depth.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.