Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created March 21, 2010 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/339114 to your computer and use it in GitHub Desktop.
Save JoshCheek/339114 to your computer and use it in GitHub Desktop.
# based on http://gist.github.com/339028
module Base
module Plugins
def plugins
@plugins ||= []
end
def activate( plugin_name )
plugin = Base::Plugins.const_get plugin_name.capitalize
if plugin
plugins << plugin_name
extend plugin
else
raise "no such plugin #{plugin_name}"
end
end
module Backward
def say(what)
super what.reverse
end
end
module Loudness
def say(what)
super what.upcase
end
end
end
end
module Base
class Server
include Plugins
def say(what)
p "say: #{what}"
end
end
end
s = Base::Server.new
s.say 'hello world'
s.activate 'backward'
s.say 'hello world'
p s.plugins
puts
s2 = Base::Server.new
s2.say 'hello world'
s2.activate 'loudness'
s2.say 'hello world'
p s2.plugins
puts
s2.activate 'backward'
s2.say 'hello world'
p s2.plugins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment