Skip to content

Instantly share code, notes, and snippets.

@jnicklas
Created February 3, 2009 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save jnicklas/57480 to your computer and use it in GitHub Desktop.
Save jnicklas/57480 to your computer and use it in GitHub Desktop.
class SomeClass
class << self
def hooks
@hooks ||= Hash.new([])
end
def add_hook(kind, &block)
self.hooks[kind] << block
end
end
def hook(kind)
self.class.hooks[kind].each { |block| instance_eval(&block) }
end
def foo
puts "Monkey"
hook :foo
end
def baz
puts "Ape"
end
end
s = SomeClass.new
s.foo # => Monkey
SomeClass.add_hook :foo do
puts "Donkey"
baz
end
s.foo # => Monkey
# Donkey
# Ape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment