Ruby access derived class "class methods" from the base class

I have a base class in Ruby that has a class method that it inherits. I would like to name this method in the base class, but pass it an option that is defined by the derived class, for example:

class Base < SuperDuperClass
  super_duper_class_method :option => my_option_value

  def self.my_option_value
    raise "Method my_option_value must be overridden by all subclasses"
  end
end

class Derived < Base
  def self.my_option_value
    "My Derived Option Value"
  end
end

However, this does not work. I believe because the top-level code in the base class is executed before the top-level code in the derived class, so the derived method is not defined when super_duper_class_method is called. I would prefer not to call super_duper_class_method in all derived classes, but just to indicate instead.

Any ideas?

+3
source share
2 answers

, super_duper_class_method . , , , , , - .

0

, , . , , ( my_option_value).

, SuperDuperClass send on self, .

class SuperDuperMethod
  def self.some_code
    self.class.__send__(options[:option])
  end
...
0

All Articles