About Ruby and object model

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 ...

Inside the object model

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

book.rb


class Book
 def name
  @name = 'metapro-ruby'
 end
end

book = Book.new
book.class          # => Book

Instance variables

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.rb


book.name
book.instance_variables         # => [:@name]

About the method

In addition to instance variables, objects have methods

book.rb


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.

Summary

--The instance variable of the object exists in the object --The object's method (instance method) exists in the object's class

About class

Classes are objects, and what applies to objects also applies to classes Like objects, classes have classes, which are Class classes.

book.rb


'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

module

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

constant

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"

Summary

--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.

Recommended Posts

About Ruby and object model
Explanation about Ruby Range object
About Ruby hashes and symbols
About Ruby classes and instances
Explanation about Ruby String object
About Ruby single quotes and double quotes
About Ruby product operator (&) and sum operator (|)
About object-oriented inheritance and about yield Ruby
Explanation about Array object of Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
Class and model
About Ruby symbols
About ruby ​​form
About Ruby Hashes
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
About object orientation
About Ruby variables
Ruby and Gem
About Ruby methods
[Ruby] "Reference to object" and "Contents of variable"
About Ruby Kernel Module
About Ruby error messages
[Ruby] Classes and instances
About Ruby exception handling
Symbols and Destructive Ruby
About Ruby Hashes (continued)
Model and table creation
[Ruby] Big Decimal and DECIMAL
About Bean and DI
[ruby] About here documents
About classes and instances
About Ruby if statement
About gets and gets.chomp
Ruby Learning # 31 Object Methods
About Ruby instance methods
About redirect and forward
Ruby inheritance and delegation
About encapsulation and inheritance
[Ruby] About instance generation
About Serializable and serialVersionUID
About the [ruby] operator
Thinking about logic Ruby
[Ruby] I thought about the difference between each_with_index and each.with_index
About the difference between classes and instances in Ruby
A rough note about Ruby arrays and hash objects
About Java setters and getters. <Difference from object orientation>
Be careful about Ruby method calls and variable references
About for statement and if statement
About synchronized and Reentrant Lock
GraphQL Ruby and actual development
[Ruby] Questions and verification about the number of method arguments
Ruby syntax errors and countermeasures
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
[Java] About String and StringBuilder
About the same and equivalent
Ruby C extension and volatile