What does :: indicate in Rails 3?

I am implementing the API in Rails 3 and notice an example of a controller defined as class Api::ToursController < ApplicationController . Does anyone know what colons mean? Is this inheritance? Or does this indicate a TourController extension? I tried to find the answer, but came up with nothing.

Here is what I mention: https://github.com/nesquena/rabl/wiki/Set-up-rabl-for-Ruby-on-Rails

+4
source share
3 answers

:: is a scope resolution operator (i.e. a namespace operator) in many languages, including C ++ and Ruby, so it is not specific to Rails.

In Ruby, modules define namespaces, so you can see code like this:

 Net::HTTP.get 'stackoverflow.com' 

Which calls the get class method of the HTTP class in the Net module.

In Rails, namespaces allow you to better organize your code (for example, to separate your API controllers from the rest) and are implemented as modules.

+5
source

Api::ToursController indicates that there is an external module named Api that contains the ToursController class. :: is the namespace operator.

Sometimes you see the name of the module preceded by ::, for example. ::Something , this means that Ruby will look in the external namespace (Main) for a class or module called Something. This usually happens when you are somewhere in the source code of a gem and refer to an external class or module.

You can do include Api to include everything in the Api module at the current level, so you do not need a namespace operator, and can use the ToursController without prefixing it with "Api ::" p>

+1
source

This is a namespace! Module :: Class.method

0
source

Source: https://habr.com/ru/post/1383786/


All Articles