Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a subclass method from a superclass

Preface: This is in the context of a Rails application. The question, however, is specific to Ruby.

Let's say I have a Media object.

class Media < ActiveRecord::Base
end

I've extended it in a few subclasses:

class Image < Media
  def show
    # logic 
  end
end

class Video < Media
  def show
    # logic 
  end  
end

From within the Media class, I want to call the implementation of show from the proper subclass. So, from Media, if self is a Video, then it would call Video's show method. If self is instead an Image, it would call Image's show method.

Coming from a Java background, the first thing that popped into my head was 'create an abstract method in the superclass'. However, I've read in several places (including Stack Overflow) that abstract methods aren't the best way to deal with this in Ruby.

With that in mind, I started researching typecasting and discovered that this is also a relic of Java thinking that I need to banish from my mind when dealing with Ruby.

Defeated, I started coding something that looked like this:

def superclass_method
  # logic
  this_media = self.type.constantize.find(self.id)
  this_media.show      
end

I've been coding in Ruby/Rails for a while now, but since this was my first time trying out this behavior and existing resources didn't answer my question directly, I wanted to get feedback from more-seasoned developers on how to accomplish my task.

So, how can I call a subclass's implementation of a method from the superclass in Rails? Is there a better way than what I ended up (almost) implementing?

like image 940
Shaun Avatar asked Jan 30 '11 22:01

Shaun


People also ask

How do you call subclass method from superclass in SAP ABAP?

A super class reference variable can hold a subclass reference variable . This superclass can call methods which are defined in the superclass only . data lr_animal type ref to zanimal. data lr_lion type ref to zlion.

How do you call a subclass method?

Base has only method() Subclass has method() and specialMethod() the method specialMethod() is the one I want to call. It's generally a bad idea. If you know the object is of type Subclass, then refer to it as that and you have no problem. If the method really belongs in Base - put it in Base.

How do you define a subclass from a superclass?

Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.

How do you call a method in superclass?

Use of super() to access superclass constructor As we know, when an object of a class is created, its default constructor is automatically called. To explicitly call the superclass constructor from the subclass constructor, we use super() .


1 Answers

Good question, but you are making it too complicated. Keep in mind a few principles and it should all be clear...

  • The types will be resolved dynamically, so if a show exists anywhere in the object's class hierarchy at the moment it is actually called then Ruby will find it and call it. You are welcome to type in method calls to anything that may or may not exist in the future and it's legal ruby syntax and it will parse. You can type in an expression that includes a reference to this_will_never_be_implemented and no one will care unless it actually gets called.

  • Even in Java, there is only one actual object. Yes, you may have a method in the superclass that's calling a method, but it is an instance of the derived class (as well as an instance of the base class) and so you can count on the new show being called.

  • In a sense, every Ruby class is an abstract class containing stubs for every possible method that might be defined in the future. You can call anything without access qualifiers in the base class or derived class.

If you want a null superclass implementation, you may want to define one that does nothing or raises an exception.

Update: Possibly, I should have just said "call show like any other method" and left it at that, but having come this far I want to add: You can also implement show with Ruby's version of multiple inheritance: include SomeModule. Since you are obviously interested in Ruby's object model, you might implement your attribute with a mixin just for fun.

like image 55
DigitalRoss Avatar answered Sep 21 '22 05:09

DigitalRoss