I would like to write a method in ruby that takes a class with certain methods and modifies its behavior by adding methods or changing how existing methods work. I would like to do this in a way that doesn’t modify the base class so basically I want a function that takes a class and returns a new modified class without harming the initial class. I’m pretty sure this is possible but I’m not sure where to start.
Share
You have a couple options:
x = Class.new(Parent) { def meth; puts "hello"; super; puts "bye"; end }to dynamically define a class and override methods (& define new ones)DelegatorSo for instance, if you wanted to dynamically create classes that logged certain method calls:
Note that you need ruby 1.9 to support capturing
do |&blk|(capturing blocks in block arguments).