[Ruby] About Ruby and object model

2 minute read

I learned about the basic language specifications of Ruby such as objects, classes and methods again in the Metapro book, so I wrote it down as a memorandum

In Ruby’s meta-pro, it seems to operate language elements such as classes, modules and instance variables.

Inside the object model

What is an object model? A way of understanding an object that is a collection of data related to a program as a model. Run the following code

book.rb


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

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

Instance variable

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 do not call the name method, the instance variable of book will not exist

book.rb


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

About #### method Objects have methods in addition to instance variables

book.rb


book.name
book.methods.grep(/name/) # => [:name]

At this time, it is not necessary for the book to have a name method. The object only has references to instance variables and classes

The name method is an instance method of Book class, and an object (instance) of Book class is required to call it. Even if the same method is focused on a class, it is called an instance method, and if it is focused on an object, it is called a method.

Summary

  • Object instance variables exist in the object
  • Object method (instance method) exists in object class

About class

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

book.rb


'metapro-ruby'.class # => String
String.class # => Class

Ruby class Class instance is the class itself Classes also have methods, which are class class instance methods.

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 on 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, all constants in a program are arranged in a tree like a file system (modules and classes are directories, constants are like files).

module MyModule
 MyConstant ='OUTER CONSTANT'
 
 class MyClass
  MyConstant ='INNER CONSTANT'
 end
end

MyModule::MyConstant # => "OUTER CONSTANT"
MyModule::MyClass::MyConstant # => "INNER CONSTANT"

Summary

  • Object methods exist in the object’s class, not in the object, and are called class instance methods.
  • A class is an object (instance of Class class) with a list of instance methods and a reference to the superclass.

~~・ The Class class is a subclass of the Module class, and the class is also a module ~~

  • Class class is a subclass of Module class, but there is a difference even though it is a subclass so that a class cannot be included in another class. In the case of Ruby, it cannot be said that a class is a type of module.

It was corrected by a comment from @scivola. Thank you very much.

*Since I am ignorant about languages other than Ruby and I do not know the difference between languages, please forgive me …

Tags:

Updated: