[RAILS] Ruby Basics 2 ~ Review of confusing parts ~

Method

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.

class

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.

instance

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

Predefined classes in Ruby

Ruby has predefined classes. String class of string object Array class of array objects Integer class for numeric objects Hash class of hash object

new method

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.

Class method

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

Instance method

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.

Difference between class method and instance method
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)

Class variables

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

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

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.

initialize method

The initialize method can automatically execute the process you want to execute at the same time you create the instance.

Class inheritance

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.

p method

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.

for statement

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.


Recommended Posts

Basics of Ruby ~ Review of confusing parts ~
Ruby Basics 2 ~ Review of confusing parts ~
Basics of Ruby
Review of Ruby basic grammar
Ruby basics
Ruby basics
Basics of sending Gmail in Ruby
Ruby Review 2
Extraction of "ruby" double hash * Review
[Ruby] Review about nesting of each
Ruby basics
Ruby Review 1
[Ruby] Summary of class definitions. Master the basics.
Ruby on Rails ~ Basics of MVC and Router ~
NIO.2 review of java
Arrangement of screen parts
Ruby on Rails basics
definition of ruby method
Basics of try-with-resources statement
Ruby cherry book review
NIO review of java
[Ruby] Class nesting, inheritance, and the basics of self
Review the basic knowledge of ruby that is often forgotten
[Rails] Introduction of devise Basics
Docker monitoring-explaining the basics of basics-
Basics of character operation (java)
[Ruby] Various types of each
Understand the basics of docker
The basics of Swift's TableView
[Ruby] List of basic commands
Judgment of fractions in Ruby