0

I have a class that inherits from another class. The setup of the superclass is as follows:

class Creator::BaseResource < Creator::Base
  def request_attributes(action = :create)
    "super"
  end
end

And the subclass:

class Creator::Resource::HypervisorGroup < Creator::BaseResource
  def request_attributes(action = :create)
    "sub"
  end
end

Now, if I have an instance of "Creator::BaseResource", and there is a DB column named "resource_class" which contains "Resource::HypervisorGroup" that makes it known that its a subclass, I would like to be able to say object.request_attributes and get "super" returned, in this example.

The code is far more complicated than this, obviously, but that's the gist of what I want to accomplish. Is it possible? Thank you.

1
  • object refers to what in object.request_attributes? Object of Creator::BaseResource or Resource::HypervisorGroup
    – Kirti Thorat
    Mar 25, 2014 at 22:19

1 Answer 1

0

What about that :

class Creator::Resource::HypervisorGroup < Creator::BaseResource
  def request_attributes(action = :create, use_super=false)
    super and return if use_super
    "sub"
  end
end

And then, you can call object.request_attributes(:create, true) to get the superclass method called

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.