Reputation: 9826
I read here that a ruby class can only inherit from one class, and can include
modules.
However, the devise module defines controllers like this:
class Users::PasswordsController < Devise::PasswordsController
...
end
Now, given that Users
is probably a class, with PasswordsController
being a method:
>> Devise::PasswordsController.class
=> Class
How is it that a method in a class inherits from another class?
Upvotes: 0
Views: 420
Reputation: 36101
What confuses you here is that you have wrong assumptions, namely:
Users
is probably a class
Not necessarily. Here we have namespace with nesting, therefore Users
can be either a class or a module. In fact classes are modules.
PasswordsController
being a method
PasswordsController
here is a class nested in the Users
namespace. ::
simply lets you go one level into the nesting tree.
Consider:
module Foo
class Bar
end
end
Foo::Bar.class # => class
Upvotes: 1
Reputation: 1265
class Users::PasswordsController < Devise::PasswordsController
...
end
In the above code, Users is the module and PasswordsController is the class inside Users module. Similarly Devise is the module and PasswordsController is the class inside Devise module.
so when you run
Users::PasswordsController.class
#=> Class
Users.class
#=>Module
Upvotes: 2
Reputation: 17
Both Users and Device are modules, just used for scoping the real classes that are PasswordsController and PasswordsController.
Upvotes: 0
Reputation: 8888
From Rails naming convention, Users
is most probably a module, and Users::PasswordsController
is a class.
Note that ::
is not for calling class methods (although it can be used this way). It's for accessing constants inside a module/class. For example
module Foo
BAR = 'bar'
end
Foo::BAR
#=> "bar"
In Ruby, a module/class name is a constant, and the value stored in it is the module/class. So ::
also is used for accessing a module/class inside another module/class. For example
module Foo
class Bar
end
end
Foo::Bar
#=> Foo::Bar
Upvotes: 0