I learned about Ruby's basic language specifications such as objects, classes, and methods again in the metapro book, so make a note as a memorandum.
Ruby's metapro seems to operate language elements such as classes, modules, and instance variables ...
What is an object model? The idea of grasping an object that summarizes data related to a program as one model. Execute the following code
class Book def name @name = 'metapro-ruby' end end book = Book.new book.class # => Book
Instance variables are contained in the object There is no connection between Ruby object classes and instance variables Instance variables only appear when a value is assigned If you don't call the name method, the book instance variable will not exist.
book.name book.instance_variables # => [:@name]
In addition to instance variables, objects have methods
book.name book.methods.grep(/name/) # => [:name]
At this time, it is necessary to note that the name method does not exist in the book. Objects only have instance variables and references to classes
The name method is an instance method of the Book class, and an object (instance) of the Book class is required to call it. Even if the same method is used, it is called an instance method when focusing on a class, and it is called a method when focusing on an object.
--The instance variable of the object exists in the object --The object's method (instance method) exists in the object's class
Classes are objects, and what applies to objects also applies to classes Like objects, classes have classes, which are Class classes.
'metapro-ruby'.class # => String String.class # => Class
An instance of a Ruby class class is the class itself The class also has methods, and the methods of the class are instance methods of the Class class.
Class.instance_methods(false) # => [:allocate, :new, :superclass] Array.superclass # => Object Object.superclass # => BasicObject BasicObject.superclass # => nil
The Object class also has methods that can be used with any object The BasicObject class is the root of the Ruby class hierarchy and has some basic methods
The superclass of Class is Module To be precise, a class is a module that adds three instance methods (new, allocate, superclass) for creating objects and inheriting classes.
Class.superclass # => Module
All capitalized references are constants, including class and module names. The important difference between constants and variables is scope, and all the constants in your program are arranged in a tree like a file system (modules and classes are directories, constants are files).
module MyModule MyConstant = 'OUTER CONSTANT' class MyClass MyConstant = 'INNER CONSTANT' end end MyModule::MyConstant # => "OUTER CONSTANT" MyModule::MyClass::MyConstant # => "INNER CONSTANT"
--The method of the object exists in the class of the object, not the object, and is called the instance method of the class. --A class is an object (instance of Class class) with a list of instance methods and a reference to a superclass.
~~ ・ The Class class is a subclass of the Module class, and the class is also a module ~~
--The Class class is a subclass of the Module class, but there is a difference even though it is a subclass so that the class cannot be included in other classes. In the case of Ruby, it cannot be said that a class is a kind of module.
I received a comment from @scivola and corrected it. Thank you very much.