Ruby get inheriting class

16,549

Solution 1

If you wish to get the name of the class you're in at the moment, you can use self.class.name. If you wish to get the superclasses, you can use the array self.class.ancestors, e.g. self.class.ancestors[1].name. The immediate superclass is also available as self.superclass.

Edit, added example:

class ControllerTest
  attr_accessor :controller
  def initialize
    super
    @controller = eval "#{self.class.name}Controller.new(self)"
  end
end

class Foo <ControllerTest
end
class Bar <ControllerTest
end

class Controller
  def initialize (o)
    puts "Created #{self.class} to control #{o.class.name}"
  end
end

class FooController <Controller
end
class BarController <Controller
end

foo = Foo.new
bar = Bar.new

This outputs

Created FooController to control Foo
Created BarController to control Bar

Solution 2

I believe you'll need to catch the classes as they're being defined...

From the RDoc for the Class class:

inherited(subclass)
Callback invoked whenever a subclass of the current class is created.

Example:

   class Foo
      def self.inherited(subclass)
         puts "New subclass: #{subclass}"
      end
   end

   class Bar < Foo
   end

   class Baz < Bar
   end
produces:

   New subclass: Bar
   New subclass: Baz
Share:
16,549
Brian DiCasa
Author by

Brian DiCasa

Web application developer with a passion for technology. Focus on web standards and server architectures. Enjoy the creation of complex applications that push the boundaries of what the web can do. Specialties include: Web Applications, Java, Server Architectures, Web Standards (HTML, CSS, JavaScript), JavaScript Libraries/Frameworks

Updated on June 05, 2022

Comments

  • Brian DiCasa
    Brian DiCasa almost 2 years

    I'm working on creating my first plugin for rails. I'm still pretty new to ruby and I was wondering if its possible to get the inheriting class?

    For example, I'm trying to create a plugin that will allow unit testing and functional testing when you are not using migrations. What I'm trying to do is initialize a class variable named controller to be initialized to the type of controller that is being tested.

    If I have a base class ControllerTest:

    class ControllerTest < Test::Unit::TestCase
      attr_accessor :controller
    
      def initialize
        super
        @controller = "call function that will find the inheriting classes name and create an instance of that controller type."
      end
    end
    

    So what I'm currently stuck on is getting the name of the inheriting class. Is this possible? And if not, does anyone know another way on how I could go about implementing this?

    Thanks in advance.