Skip to content
A simple event/notification mixin for Ruby.
Find file
New pull request
Failed to load latest commit information.
script Ditch Rake
test Spike all-instances listeners
.gitignore Pull in bundler/setup
Gemfile Ditch Rake
LICENSE Break out a LICENSE file Spike all-instances listeners
watchable.gemspec Bump mocha


A simple event/notification mixin, reluctantly extracted to a gem. This is code I've had floating around for a few years now, but I've also incorporated a few extras from node.js' EventEmitter, jQuery, and Backbone.Events.



require "watchable"

class Frob
  include Watchable

class Callable
  def call *args
    p :called! => args

Watching and Firing

Events can have any number of watchers. Each watcher will be called in order, and any args provided when the event is fired will be passed along. Watchers will most commonly be blocks, but any object that responds to call can be used instead.

frob =

frob.on :twiddle do |name|
  puts "#{name} twiddled the frob!"

frob.on :twiddle do |name|
  puts "(not that there's anything wrong with that)"

frob.on :twiddle, :twiddle, "John"


John twiddled the frob!
(not that there's anything wrong with that)
{ :called! => ["John"] }

Watching Everything

Want to see every event fired by an object? Register for :all. The first argument will be the name of the fired event.

frob =

frob.on :all do |event, culprit|
  p :fired => [event, culprit]
end :foo, "John"


{:fired => [:foo, "John"]}

Watching Every Instance of a Class

To see all events fired by instances of a class, register for events on the class constant. The first argument will be the name of the event, and the second the instance that fired it.

Frob.on :foo do |event, instance, culprit|
  p :fired => [event, instance, culprit]

frob = :foo, "John"


{:fired => [:foo, #<Frob:0x007f9828e9e608>, "John"]}

Watching Once

Only want to be notified the first time something happens? once is like on, but fickle.

frob =

frob.once :twiddle do
  p :twiddled!
end :twiddle :twiddle




Specific blocks or callable objects can be removed from an event's watchers, or all the event's watchers can be removed.

b    = lambda {}
frob =

frob.on :twiddle, &b :twiddle, b  # removes the 'b' watcher, same as :twiddle, &b :twiddle     # removes all watchers for the 'twiddle' event


Watchable is actively developed against MRI Ruby 1.8.7 as a least common denominator, but is widely tested against other Ruby versions and implementations. Check the travis-ci page for details.

Something went wrong with that request. Please try again.