This will be a memo for learning.
In Ruby, many terms such as "object" and "variable" come out and it is difficult, so I would like to output it in my own way to speed up understanding even little by little. If there are any differences, I would appreciate it if you could comment.
To understand classes and instances, we need to understand the terms "object" and "method".
The explanation is below ...
An object is " all data in Ruby "
.
For example, characters such as "good morning" are called "character string objects", and numbers such as "123" are called "numeric objects".
The "processing" of an object is called a method. Every Ruby object has a "method". For example, like this
-Character string object-> length method-> count the number of characters -Numeric object-> to_s method-> Convert to integer
The methods that each object type has are different. For example, each person has a different personality.
A class is a " collection of common properties and methods "
, and this time, we will consider a "car blueprint" as an example.
When you make a car, you need a blueprint, but all cars have something in common (such as "accelerating" with the steering wheel and brakes). That is, "let's put together the common parts in a blueprint."
In other words, it is a mechanism that allows you to create a class (blueprint) so that you can use it when making other cars.
An instance is a " object created by inheriting attributes and processing from a class "
.
Is it easy to understand an object (car) created based on a class (blueprint)? Since it inherits the minimum necessary parts, it is an advantage that you do not have to make a blueprint such as "There is a handle" from scratch.
Recommended Posts