P
P
pup_pupets2016-06-21 00:48:44
Ruby on Rails
pup_pupets, 2016-06-21 00:48:44

How are these structures different?

There are two pieces of code:

module Admin
  class CategoriesController < AdminController

and
class Admin::CategoriesController < ApplicationController

What is the difference? In both cases, everything seems to work as it should. And which one is correct?
PS I understand that there is no correct one and both are identical.
Sorry, I'm still learning.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Sidorov, 2016-06-21
@pup_pupets

There are differences. In the first case, the code inside the class will see the constants from the module (and the names of nested classes, respectively), but not in the second.
Example:

module A1
  TEST = 'zz'
end

module A1
  class B1
    def foo
      TEST
    end
  end
end

module A2
  TEST = 'zz'
end

class A2::B2
  def foo
    TEST
  end
end

A1::B1.new.foo #> "zz"
A2::B2.new.foo #> NameError: uninitialized constant A2::B2::TEST

P
Pavel Grudinkin, 2016-06-21
@Hunt666

first google link for "rails modules": rubyclub.blogspot.ru/2012/10/ruby_869.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question