If you're using Rails, you've probably seen the following description:
models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
#Omitted below
You inherited ActiveRecord :: Base
from Application Record
, right?
Nothing has changed.
・ ・ ・
・ ・
・
Well, eh, wait a minute.
What is it, ::
! ?? !! ?? !! ??
scared! !! !!
So, as a Ruby beginner, I will leave a memorandum of research on ::
.
::
has two features.
Specify the absolute path of the namespace
module Hoge
class Fuga
def self.fuga
p "fuga"
end
end
end
Hoge::Fuga.fuga #=> "fuga"
Fuga.fuga #error
#Create Fuga class
class Fuga
def self.fuga
p "fuga"
end
end
Hoge::Fuga.fuga #=> "fuga"
Fuga.fuga #=> "fuga"
Constant scope operator
Use ::
to reference constants defined in a class or module from outside the scope.
class Hoge
FUGA = "FUGA" #Constant FUGA
p FUGA #=> "FUGA"
end
#Output constant FUGA from outside the scope.
p Hoge::FUGA #=> "FUGA"
Also, ** Ruby treats classes and modules as constants. ** ** For example, when you create a Hoge class, the Hoge class object is assigned to a constant ** named ** Hoge.
::
of ActiveRecord :: Base? ??The ::
in ActiveRecord :: Base points to the ** Base class in the ActiveRecord module! !! ** **
It's a Base class nested in ActiveRecord.
The simple description is as follows.
active_record/base.rb
module ActiveRecord
#abridgement
class Base
#abridgement
end
#abridgement
end
The official contents of ActiveRecord :: Base are as follows. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb
This is a deeper summary of ActiveRecord features. Rails: ActiveRecord :: Base Method Summary
Official Ruby Reference Variables and Constants https://docs.ruby-lang.org/ja/latest/doc/spec=2fvariables.html#const
Rails Guide Active Record Basics https://railsguides.jp/active_record_basics.html