The objects that can be used for the method are fixed. In the case of each method, it can be used only for array objects and range objects. Also, when the each method is used for an array object, the return value is the array object itself.
A class is like a type that puts together the common attributes and methods of an object. "Hello" and "Hello" is a separate object, but it is common in that say to have a character. This property is defined in advance as a property of the class. If you prepare this type in advance, you can efficiently create objects along the shoulder.
An object born from a class is called an instance. Objects are not born first, but classes give rise to objects called instances. An instance is created with attributes, attribute values and methods. In other words, the attribute is defined in the class, and the instance is created with the specific attribute value.
For example, the string object "Hello" is instantiated with the attribute = character defined in the String class, with the attribute value = "Hello" in it, and with methods such as the length method and to_i method. ..
Ruby has predefined classes. String class of string object Array class of array objects Integer class for numeric objects Hash class of hash object
The new method can be used without defining it in all classes. You can create an instance by using the new method. Returns an instance of the class used as the return value. It is a class method. Because the class should create the instance.
The class method can be used by the class that defines the class method itself, and is used for processing that uses common information in the class.
Prefix the method name with
Instance methods are the methods available to your instance. Can be used for an instance of the class that defines the instance method. It can be used for processing that uses individual information for each instance.
|Feature||Instance method||Class method|
|Definition method||Do not prefix the method name with self||Prefix self to the method name|
|Use||When using per-instance attributes||When performing common processing where attributes are not related|
|Objects that can be called||Instance of class||Class itself|
|How to call||Instance name.Method name (argument)||name of the class.Method name (argument)|
Common attributes in classes are defined using variables. The value assigned to that variable is the attribute value. Variables that can be defined in a class include class variables and instance variables.
Class variables are variables that can be used throughout the class. That is, it can be used both within class methods and within instance methods. Variables used for information whose values are common throughout the class.
Instance variables are variables that can be defined in an instance as common attributes. The value can be set for each instance. Only available on each instance. The definition location is done in the instance method. The value of the instance variable defined in the instance method is the value of the instance variable of the instance that uses the instance method.
The initialize method can automatically execute the process you want to execute at the same time you create the instance.
Making a method defined in one class available to another class is called inheritance. The class you want to inherit is called the parent class, and the inheriting class is called the child class. class Child class name <Parent class name can inherit the parent class.
The p method outputs the objects and instances written to the right of p. Similar to the puts method, but the puts method returns the return value nil, while the p method returns the object or instance itself.
A grammar that repeats the same as a while statement.
for num in 1..10 do puts num end The variables num are assigned 1 to 10 and output. The variables num are assigned in the order of the objects after in. 1..10 is 1~It means 10.