"class Admin::UsersController < ApplicationController". What does the double colon do here?

I saw this on a Gem implementation and don’t understand why it needed
“::UsersController” at all.

My understanding that it is trying to find the relative path of
UsersController, but why is it necessary to be there?

This is one of the ways to provide namespacing in Ruby and avoid
same-name
conflicts (e.g. with user code or other gems).

class Admin::UsersController …
end

is equivalent to

module Admin
class UsersController …
end
end

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Hi,

On 25.03.2014 09:53, Konstantinos Rousis wrote:

class Admin::UsersController … end

is equivalent to

module Admin class UsersController … end end

Only partly right. There is a slight difference.

As long as the module (or namespace) alread exists you are right. But
if you didn’t encounter a „module Admin“ statement before the „class
Admin::UsersController“ will fail with an „NameError: uninitialized
constant Admin“. So

module Admin
class UsersController
end
end

will always create the module if it doesn’t exist, yet while

class Admin::UsersController
end

will not do this and error out.

Best regards,


Christian Kruse http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQQcBAEBCAAGBQJTMVTWAAoJEEnjDsXITviXW5wf/2Br1LhMsS7bOc4d4C8MbKfi
r9l30m3GOjz0YCi2on9zIhpIrLS489bMtvnSvs2CAXA3DVzZpPl6TKaq8ttJR2HW
c6PsWgv0ocoIW/O2O3p2Cd808QRAU8lwTOLiuo9WYxVLN44+z6nbENrCdqRlfQ3B
4MaAtvMywZ8YgM6TtTc5rdk+o/9qknDyk2tu4TzoAOkUfwY+0F7uY/uXSh5aNirg
Js3kX4SQ8/H9sRlwHwP6cRM0JWZ5PnOCutpCVOR2RSygLKtW+9RJc21HIBUsCS+Y
zR+Ksn9qNPcTMUnpdtqR2gEbtkEZsqMU7sFtF8paBaA2Vxa82hBiku2maBo8glle
0tFOc04++Nir64sREGQmQDNx+W+OUbNliuidJWJGg3IAfKRkpPAP847Y7GQseier
NQwSqPDpgQAOhhX66va5u5LLvNhww0AuqeezVLnvDns5gLopougv8xfknKJxhtxo
5TtnNKTBznvF8bVQ+Dk+f8UsA0XjjYPCqRwYdLK1bGj7XJP0Q2UHEcvlhSmtQufu
D3mM5gARKN66t8khx9O6hV6VrPKlhzVcUfDzmiArquR+erABLjg3JIvtugLheJ4w
djSQE7WVBVbjIYYq/l7I9Jdk49tuLVwZ77ywKz/RYKQNc09LTbJizaE7gw3Ia2ZU
zd/3pb1LWCbvVIBP+7PZyVez+N4wvUuNwTCGuFLXj5pCD3cgRoiuX/60rtX3GOL6
jM8G/SvkYeP4EhvobuuUDp4MWlQluFwwzSJJ8A1N8ue0uemKhJdN0FRGNURVh2Qb
WyNIi9r4AVT8N8RkrR9I4qkRl/UT6Or3wmG/Wdm9CcxVjKHW+KuMwufLgqkpWUrK
HEjJd66dGVy5o/x66jLXawmB3YizPp7UFppMT0lrLTqQ5/mpHhuaFOzH+FHjPBfB
IxaPDtNKYmOieIDCpZiGDig0wy+9IVf3fCutel6hwTWxwm47DtEOKWl1udDDs6yh
uonzrURQiGDQFR/D8wWn6YNzTmPRityRryz4GX1DORoYbalhst9a08U6RmgWSRTo
w2qdjpy0YycO/Gm5Ejd8byuhdUgJo+1/P4c2LG9cJq4omOD7NKEmVCQ8VKmpA03R
FMNbWSOiZrjQD8uIiXu/67xaJsfBeX7xwbokJHb813h5SzBRwMDbxCQNnk8YN3gY
9LXK+lT4Ps977y7NhxQQzVGhH3HHJgokkrkg9zezX1BMgFluxsWI7ikbKDGvXV9r
0c+tAfIuQCsFVKSMO0Uxid0qvMMpUCli5uE0gTOsekkIPHWXiE5uEq/JnMCUh7Oh
gf9F4gJqzhuA7tT7vbMRQW11tNcCdo/p9XSiwNaoGKgyQNe1Sf+4acN0h/jtqyE=
=Msc8
-----END PGP SIGNATURE-----

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs