Skip to content

Instantly share code, notes, and snippets.

@stuartellis
Created February 10, 2011 19:09
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 stuartellis/821123 to your computer and use it in GitHub Desktop.
Save stuartellis/821123 to your computer and use it in GitHub Desktop.
Code sample for using Ruby modules as plugins
#!/usr/bin/env ruby -wKU
class Item
attr_accessor :title
def initialize(title)
@title = title
end
end
module PluginA
def title()
@title = @title + ' Handled by A'
super
end
end
module PluginB
def title()
@title = @title + ' Handled by B'
super
end
end
module Stream
extend self
def load_plugins(items, plugins)
items.each do |i|
plugins.each do |p|
i.extend(const_get(p))
end
end
end
end
plugins = ['PluginA', 'PluginB']
items = [Item.new('First Item '), Item.new('Second Item')]
ready_items = Stream.load_plugins(items, plugins)
ready_items.each do |item|
puts item.title
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment