Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A port of Scala's implicits, this is an alternative to open classes that allows for easier testing.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 implicitly.gemspec
README.rdoc

Synopsis

Implicits in Ruby, based on Scala. Works like:

implicitly.from(BaseClass).to(WrapperClass).via do |base_object|
  WrapperClass.new(base_object)
end

Example

For example, to extend Fixnum with #seconds, #minutes, and #hours:

class FixnumWithTime
  def initialize(n)
    @n = n
  end

  def minutes
    seconds * 60
  end

  def hours
    minutes * 60
  end

  def seconds
    @n
  end
end

class Person
  implicitly.from(Fixnum).to(FixnumWithTime).via do |fixnum|
    FixnumWithTime.new(fixnum)
  end

  def age_in_seconds
    37.minutes
  end
end

Why

By using a wrapping class the extension can be tested independently of the base class.

Author

Mike Burns mburns@thoughtbot.com mike-burns.com/

Something went wrong with that request. Please try again.