[Ruby] Class methods, instance methods, etc.

Introduction

Last time, I wrote articles about class inheritance and nesting, This time, I will write basic knowledge such as class methods.

Method

Class method

As the name implies, it is a method that can be called by a class object.

It cannot be called with an instance object. Therefore, a common contrast is the instance method. This will be discussed later.

There are the following methods for definition.

Prepend self. To the method name when defining the method

When using your own class as a receiver in a method, use class name or self. It can be omitted, but self in the definition cannot be omitted.

class Klass
  def self.method_a
    new
    puts "created Klass instance"
  end
end

Klass.method_a
# => created Klass instance
Enclose in class << self, end

Everything defined during this time is a class method This is convenient when defining multiple items.

class Klass
  class << self
    def method_a
      new
      puts "created Klass instance"
    end
  end
end

Klass.method_a
# => created Klass instance

By the way, you can do it like this in a similar way.

class Klass
end

class << Klass
  def method_a
    new
    puts "created Klass instance"
  end
end

Klass.method_a
# => created Klass instance

Instance method

A method that can be called by an instance object. This is in contrast to the previous class method It cannot be called from a class object.

The definition method is as follows.

class Klass
  def method_a
    puts "method_a called"
  end
  
  def method_b
    method_a
    puts "method_b called"
  end
end

klass_instance = Klass.new
klass_instance.method_b
# => method_a called
# => method_b called

Singular method

An object-specific method. It can be defined by writing the object name before the method name to be defined. Note that the object must already exist when you define it.

class Klass
end

apple = Klass.new
orenge = Klass.new

def apple.tokui_method
  puts "I'm apple"
end

apple.tokui_method
# => I'm apple
orenge.tokui_method
# => undefined method `tokui_method'

If the specified object does not exist when defining it, an error will occur.

class Klass
end

def apple.tokui_method
  puts "I'm apple"
end

apple = Klass.new

apple.tokui_method
# => undefined local variable or method `apple' for main:Object (NameError)

Calling super within a singular method will call the method of the same name defined in the class. There is also the convenience of being able to override that method.

class Klass
  def my_name
    puts "I'm fruits"
  end
end

apple = Klass.new
orenge = Klass.new

def apple.my_name
  super
  puts "I'm apple"
end

apple.my_name
# => I'm fruits
# => I'm apple
orenge.my_name
# => I'm fruits

end

Recommended Posts

[Ruby] Class methods, instance methods, etc.
[Ruby] methods, instance methods, etc ...
About Ruby instance methods
[Ruby] Basic knowledge of class instance variables, etc.
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Self-use memo] (Ruby) class, class instance, instance
What are Ruby class methods?
Java class methods
Class in Ruby
String class methods
Ruby Learning # 15 Methods
About Ruby methods
Ruby Learning # 31 Object Methods
About calling instance methods
Ruby variables and methods
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Ruby] About instance generation
Find out about class methods
Basic methods of Ruby hashes
[Ruby] Does self refer to a class or an instance?
Basic methods of Ruby arrays
Three Bit Manipulation Methods (Ruby)
Instance creation, constructor, field, etc.
[Ruby] What is an instance?
[Ruby] Singular methods and singular classes
Ruby variables and functions (methods)
Ruby methods and classes (basic)
[Ruby] Singular methods and singular classes
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
Easy to understand the difference between Ruby instance method and class method.
Difference between instance method and class method
Various methods of Java String class
ruby exercise memo VI (hello class)
Write Ruby methods using C (Part 1)
Ruby standard input and various methods
Ruby convenience methods for fledgling engineers
Difference between instance variable and class variable
[Ruby] Date, Time, Datetime class basics
Various methods of the String class
How to use class methods [Java]
Ruby methods often used in Rails