First, let's look at the difference between Ruby class methods and instance methods.
It can be used by the class itself that defines the class method. It is used for processing that uses common information in the class. You can call it without an instance.
When defining a class method, write self. method name
. It can be called from the class name Fruits
as follows. Also, the new
method that creates an instance is a typical class method.
fruits.ruby
calss Fruits
def self.call #self refers to the class itself
puts "Called a class method"
end
end
Fruits.new #The new method is also one of the class methods.
Fruits.call
Running the above ruby file in a terminal gives the following result:
Terminal
ruby fruits.rb
Called a class method
The execution result of Fruits.call is displayed. Fruits.new will not be displayed in the execution result as it will only generate an empty instance.
A method that can be used by the instance and is defined in the class. You can use the instance generated from "Class that defines the instance method".
fruits.ruby
calss Fruits
def call
puts "I called an instance method"
end
end
fruits = Fruits.new
fruits.call
#.You can also connect them with and call them as follows.
Fruits.new.call
Terminal
ruby fruits.rb
I called an instance method
The initialize
method is an instance method that can define the process that is executed at the same time that the instance is created by the new
method.
Actually, the initialize
method is executed automatically even if you do not write it in the class, and in this case nothing is processed.
fruits.rb
class Fruits
def initialize
puts "Instance was created"
end
end
Fruits.new
Running the above ruby file in a terminal gives the following result:
Terminal
ruby fruits.rb
Instance was created
You can see that the process defined in the initialize
method was executed after the instance was created in Fruits.new
.
Next, let's look at the difference between class variables and instance variables.
It is a variable defined in the class and is defined by writing @@ variable name
. Class variables are suitable for use when you want to define common variables as a class.
fruits.rb
class Fruits
@@fruit = "Orange" #Defined before instantiation for use within the initialize method
def initialize
puts "I#{@@fruit}I like"
end
end
Fruits.new
Execution result
Terminal
ruby fruits.rb
I like oranges
A feature of class methods is that once defined, they can be used anywhere in the class.
fruits.rb
class Fruits
@@price = 0
def initialize(fruit, price)
@fruit = fruit
@@price += price
end
def self.sum
puts "The total amount is#{@@price}It's a yen"
end
end
Fruits.new("Orange", 200)
Fruits.new("Apple", 300)
Fruits.new("Grape", 500)
Fruits.sum
Terminal
ruby fruits.rb
The total amount is 1000 yen
The price of each fruit was added to the class variable @@ price
each time an instance of orange, apple, or grape was created, and the total amount was output by thesum
method.
A variable that can be defined in an individual instance, and is defined by writing @variable name
. Instance variables can be used in instance methods other than the defined instance methods.
fruits.rb
class Fruits
def initialize(fruit)
@fruit = fruit
puts "I#{@fruit}I like"
end
def dislike
puts "I#{@fruit}I'm not good at"
end
end
Fruits.new("Orange")
Fruits.new("Apple")
fruit = Fruits.new("Grape")
fruit.dislike
Terminal
ruby fruits.rb
I like oranges
I like apples
I like grapes
I'm not good at grapes
In the initialize
method, the value of the instance variable @ fruit
is assigned to each created instance and output by puts.
You can also see that the instance variable @ fruit
defined in the initialize
method can also be used in the dislike
method.
Active engineers explain how to use Ruby class variables [For beginners](TechAcademy Magazine)