Created
September 2, 2010 16:56
-
-
Save hannestyden/562547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module InstrumentModule | |
def self.build(*methods) | |
evil = methods.map do |method| | |
%Q[ | |
def #{method}(*arguments, &block) | |
t = Time.now | |
r = super | |
td = Time.now - t | |
puts "*inst* " + (self.class === Class ? self.name + "." : self.class.name + "#") + "#{method}: " + td.to_s + " s" | |
r | |
end | |
] | |
end | |
name = "M#{Time.now.to_f.to_s.gsub(/\W/, "")}#{rand(10000)}" | |
uber_evil = %Q( | |
module #{name} | |
#{evil.join("\n")} | |
end | |
) | |
module_eval uber_evil | |
eval name | |
end | |
end | |
class C | |
def self.m | |
sleep 0.1 | |
:Cm | |
end | |
def self.n | |
sleep 0.1 | |
:Cn | |
end | |
def m | |
sleep 0.1 | |
:m | |
end | |
def n | |
sleep 0.1 | |
:n | |
end | |
end | |
class D < C | |
extend InstrumentModule.build(:m, :n) | |
include InstrumentModule.build(:m, :n) | |
end | |
D.ancestors # => [D, InstrumentModule::M1283446573510151394, C, Object, Kernel, BasicObject] | |
D.m # => :Cm | |
D.n # => :Cn | |
D.new.m # => :m | |
D.new.n # => :n | |
# >> *inst* D.m: 0.100126 s | |
# >> *inst* D.n: 0.100163 s | |
# >> *inst* D#m: 0.100079 s | |
# >> *inst* D#n: 0.100104 s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment