2

Just curious as to why this happens in the rails console:

Loading development environment (Rails 4.1.5)
2.1.2 :001 > require 'application_controller'
 => true 
2.1.2 :002 > ApplicationController
 => ApplicationController 
2.1.2 :003 > ApplicationController.subclasses
 => [] 
2.1.2 :004 > ::ApplicationController.descendants
 => [] 

I have this controller

class MyController < ApplicationController
end

So I'm expecting ApplicationController.subclasses #=> [MyController], however, I'm getting an empty array :(

Starkers
  • 10,273
  • 21
  • 95
  • 158

1 Answers1

2

Well, credit goes to MrYoshiji, but here's how you display subclasses.:

Loading development environment (Rails 4.1.5)
2.1.2 :001 > require 'application_controller'
 => true 
2.1.2 :001 > require 'my_controller'
 => true 
2.1.2 :002 > ApplicationController
 => ApplicationController 
2.1.2 :003 > ApplicationController.subclasses
 => [MyController] 

Lazy load means an object doesn't exist in memory until it is specifically called during runtime. My second command, require 'my_controller actively references a subclass of ApplicationController and so it appears when we list its subclasses with the .subclass method.

Starkers
  • 10,273
  • 21
  • 95
  • 158