I would like to dynamically specify the parent class for a class in Ruby. Consider this code:
class Agent
def self.hook_up(calling_class, desired_parent_class)
# Do some magic here
end
end
class Parent
def bar
puts "bar"
end
end
class Child
def foo
puts "foo"
end
Agent.hook_up(self, Parent)
end
Child.new.bar
Neither the Parent
nor the Child
class definition specifies a parent class, so they both inherit from Object. My first question is: what would I need to do in Agent.hook_up
in order to make Parent
the superclass of Child
(so for example Child
objects can inherit the 'bar' method).
My second question is: do I need to pass the first argument to Agent.hook_up
, or is there some way the hook_up
method can programmatically determine the class from which it was called?