[Ruby / namespace] Deepen ":: ← this" that you often see in Ruby

Overview

I'm currently learning the programming language Ruby and its web framework Ruby on Rails. There are some symbols that you often see in it.

:: ← This is it.

From a beginner's point of view, this symbol was something I only thought about, "Don't look closely at class names." Of course, it makes sense.

This article will give you an overview.


:: ← What is this?

By the way, "::" that I used without being conscious of it ← This is. To understand it, we need to know the concept of namespace.

What is a namespace?

So what is a namespace? Simply put, it is the range for managing methods and variables''. Even with the same speaking'', the words are different in the United States and Japan, and even with the same Taro, one set and two sets of Taro are different people.

In this way, the namespace is a mechanism that specifies "where do the methods and variables belong?" So that conflicts due to the same name do not occur.

As you can see, ○○ :: △△ means ○○ belonging to △△.

Device :: ResistrationsController can be read as ResistrationsController belonging to Devise.

Also, namespaces are divided by "class" or "module". I won't go into detail here, but there are the following differences between classes and modules:

Regarding the points pointed out by @scivola I have made some corrections below.


Meaning of :: / Specific example

So far, we have found that :: means to call B belonging to by using A :: B.

Looking a little closer, :: has two implications:

1.Calling constants belonging to a class module
2.Calling a method that belongs to a class module

Let's look at each example.


1. Refer to the constant PI of the Math module

Let's call the constant PI belonging to the Math module, which is a built-in library of Ruby, and output the pi. (Reference: Ruby Reference | module Math )

call_pi.rb


puts Math::PI

Run this program.

terminal


% ruby call_pi.rb
3.141592653589793

I was able to output pi.

Use this when referencing a constant.

As an aside, I'll try another form of calling for later examples. Try calling it with ". (Dot)" like calling a method.

call_pi.rb


puts Math.PI

When you run this program ...

terminal


% ruby call_pi.rb
Traceback (most recent call last):
math.rb:1:in `<main>': undefined method `PI' for Math:Module (NoMethodError)

The error is `` The method called PI is not defined in the Math module''. Since PI is a constant (value), not a method, you may get an error like this.


2. Refer to the "run" method of the animal class (self-made)

Next, let's look at an example of "method reference".

In this example, to see the benefits of having a namespace, let's write without a namespace first.

run.rb


def run
  puts 'Run on 4 legs'
end

def run
  puts 'Run on two legs'
end

run
run

Let's run this program.

terminal


% ruby run.rb
Run on two legs
Run on two legs

If you reuse the same method name run, the method defined later will be referenced.

Now let's define these runs separated by the Dog Human class.

run.rb


class Dog
  def self.run
    puts 'Run on 4 legs'
  end
end

class Human
  def self.run
    puts 'Run on two legs'
  end
end

Dog::run
Human::run

Let's run this program.

terminal


% ruby run.rb
Run on 4 legs
Run on two legs

A method along the namespace was referenced. This is because by defining the class and using ::, it is possible to specify the receiver and call it like Refer to Dog's run.

By the way, in the case of this usage of :: (method call), it can be replaced with ". (Dot)". Specifically, it is as follows.

run.rb


(abridgement)
Dog.run
Human.run

terminal


% ruby run.rb
Run on 4 legs
Run on two legs

It turns out that it behaves the same as a class method call.

And if you use :: as a "method call", you can specify more than just a class or module for the receiver.

For example, like this.

sample.rb


10::times { |i| puts i }

terminal


% ruby sample.rb
0
1
2
3
4
5
6
7
8
9

That's how to use :: and what it means.


Summary

While Rails is useful, I find it surprisingly painful when trying to customize it myself. And most of the causes are `` I don't know the basics of Ruby''.

It's important to catch up in a short amount of time, but it's also important to sit down and understand the basics. I will try to keep the balance while learning.


reference

Recommended Posts

[Ruby / namespace] Deepen ":: ← this" that you often see in Ruby
Recommended site for errors that you often see
Error logging and exception handling that you can't often see in the Java area
What you write in C # is written in Ruby like this
I tried to summarize the words that I often see in docker-compose.yml
Ruby methods often used in Rails
A story about an arithmetic overflow that you shouldn't encounter in Ruby
Methods that I found useful in Ruby
[Ruby] What if you put each statement in each statement? ??