Skip to content

Instantly share code, notes, and snippets.

@bravoecho
Created October 24, 2013 11:03
Show Gist options
  • Save bravoecho/7135185 to your computer and use it in GitHub Desktop.
Save bravoecho/7135185 to your computer and use it in GitHub Desktop.
Dynamically build and include a parameterized module. Similar to the Sequel gem inheritance mechanism.
module Awesome
def self.Stuff(thingy)
Module.new do
class_methods = Module.new do
attr_accessor :thingy
end
define_singleton_method :included do |klass|
klass.extend(class_methods)
klass.thingy = thingy
end
def awesome_stuff
"I'm awesome indeed"
end
end
end
end
class NotSoAwesome
include Awesome::Stuff(:piece_of_cake)
end
NotSoAwesome.thingy # => :piece_of_cake
my_obj = NotSoAwesome.new
my_obj.awesome_stuff # => "I'm awesome indeed"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment