I am quite new to Ruby, so still learning. I was researching quite a bit about how to add methods dynamically, and I was successful to create instance methods, but not successful when creating class methods.
This is how I generated instance methods:
class B
def before_method
puts "before method"
end
def self.run(method)
send :define_method, method do
before_method
puts "method #{method}"
end
end
end
class A < B
run :m
run :n
end
Any idea about the best ways to create static methods?
My final task is to look for the best way to create “before” and “after” tasks for class methods.
Here’s something re-worked to use class methods:
Update: Using
define_singleton_methodfrom Ruby 1.9 which properly assigns to the eigenclass.