Skip to content
A simple IoC container for ruby
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Build Status Maintainability Test Coverage


Canister is a simple IoC container for ruby. It has no dependencies and provides only the functionality you need. It does not monkey-patch ruby or pollute the global namespace, and most importantly it expects to be invisible to your domain classes.

It provides:

  • Out-of-order declaration
  • Caching
  • Automatic dependency resolution
  • Automatic cache invalidation on re-registration

Why do I need this? Especially in ruby?

Canister was created to make it easier to declare the setup for an application's production and test environments in a single place, without needing to know when exactly those objects were created.

Canister is not a replacement for dependency injection. Rather, it is useful when you have designed your objects to have their dependencies injected.

The domain of your application is not concerned with the design patterns you use to implement it; therefore, the domain entities within in it should represent the domain, not the patterns. For example, it would be a mistake to write all of your classes such that they accept a single parameter called container. is undesirable when what your application really calls for is, model, year).

For more information on dependency injection and inversion of control containers in ruby, please see this excellent article by Jim Weirich that both sums up the issue and was the inspiration for this gem.


Add it to your Gemfile and you're off to the races.


# Basic usage
container =
container.register(:foo) { "foo" }
container.register(:bar) {|c| + "bar" }     #=> "foobar"
container[:bar]   #=> "foobar"
container["bar"]  #=> "foobar"

# Dependencies can be declared in any order
container.register(:after) {|c| "#{c.before} and after" }
container.register(:before) { "before" }
container.after   #=> "before and after"

# Values are cached
container.register(:wait) { sleep 3; 27 }
  #=================> 27
container.wait    #=> 27

# Caches are invalidated automatically
container.register(:foo) { "oof" }     #=> "oofbar"


Standard rules apply.


  • ruby 2.3.x
  • ruby 2.4.x
  • ruby 2.5.x
  • ruby 2.6.x

As Canister does not rely on any specific runtime environment other than the ruby core, it is compatible with every ruby library and framework.



Copyright (c) 2018 The Regents of the University of Michigan.
All Rights Reserved.
Licensed according to the terms of the Revised BSD License.
See for details.
You can’t perform that action at this time.