get_parent_class

  • We can get the parent class for an object in Ruby by first referencing the class object using the Object#class. Once we have the class object, we can use the Class#superclass method to find the superclass.

    PHP

    class Dinosaur {}
    class TRex extends Dinosaur {}
     
    $t = new TRex;
    $result = get_parent_class($t);
    var_export($result);
    // => 'Dinosaur'

    Ruby

    class Dinosaur; end
    class TRex < Dinosaur; end
     
    t = TRex.new
    p t.class.superclass
    # => Dinosaur

    see also

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.