Leave a comment

Simple Metaprogram to Dynamically inject Class Methods and Instance Methods from module

module DummyModule

module ClassMethods
def find_by_ranks()
puts “Class Method Executed Successfully !!!”
end
end

module InstanceMethods
def ranks()
puts “instance method Executed Successfully !!!”
end
end

def self.included(base)
base.send :include, InstanceMethods
base.send :extend, ClassMethods
end

end

class Test
include DummyModule
end

t = Test.new
t.ranks #output : instance method Executed Successfully !!!
Test.find_by_ranks #output : Class Method Executed Successfully !!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: