magic_options ruby gem

Rory McKinley and I have published a little ruby gem that implements what I call the magic options pattern.  You know the drill:

class BigThing
  def initialize(options = {})
    @height = options[:height]
    @width  = options[:width]
    @depth  = options[:depth]
    @weight = options[:weight]
  end
end

What an epic yawn.

With magic_options, you can just do this:

class BigThing
  include MagicOptions
  def initialize(options = {})
    magic_options options
  end
end

# Alternatively...

class BigThing
  include MagicOptions
  magic_initialize
end

If you’d like to program defensively, there are additional options for specifying what the options hash may and may not include.

Sure, it won’t save the world, but it’ll make the world a better place.

The source code is available at github.com and the documentation is already live on rubydoc.info. Use the usual gem install magic_options to install.

Tags: work