Li Xinyang Li Xinyang - 1 month ago 6
Ruby Question

Which class get inherited when using nested class

class Admin::ApplicationController < ApplicationController
def index
end
end


Which class get inherited when I using nested class?

class Admin < ApplicationController
class ApplicationController
end
end


or

class Admin
class ApplicationController < ApplicationController
end
end


I think the second one is the winner, because what I understand
Admin::ApplicationController < ApplicationController
is get the
ApplicationController
inside
Admin
namespace and make it inherit from
ApplicationController
.

Answer

If the question is what is the equivalent of this line:

class Admin::ApplicationController < ApplicationController

Then your second assumption is correct, it is equivalent to:

class Admin 
  class ApplicationController < ApplicationController
  end
end

Few sidenotes though:

  1. Your current design exposes bad naming
  2. Why not make Admin a module instead of class?
  3. Prefer using the explicit form instead of nested one - you'll never get confused.
  4. See this thread about some difference in levels of nesting between two forms of class definitions.
Comments