Difference between instance method and class method

I got confused about the title part while learning about Ruby, so I looked at them all together to organize them.

Difference between instance method and class method

class Example
  def index
  end
end
  
  
  ①example = Example.new 

example.index // Call from instance → ○

② Example.index // Cannot be called directly from the class → When using this, add self. To the method (self.index)

index // Of course this will display an error

In short, when calling a basic method, create an instance and call it from there (①). → Instance method

A method to call a method without creating an instance as in (2). → Class method

An image like that.

The advantage of using a class method is that it can be used as a method that does not include information on other instances (I did not know a specific example, so I will summarize it as soon as I understand it). First of all, it is related to the class, but it seems OK to recognize that it is an independent method that does not include information on other instances.

Recommended Posts

Difference between instance method and class method
Difference between class and instance
Difference between instance variable and class variable
Difference between variables and instance variables
Easy to understand the difference between Ruby instance method and class method.
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
Difference between == operator and eqals method
What is the difference between an action and an instance method?
Difference between Ruby instance variable and local variable
[Java] Differences between instance variables and class variables
Difference between vh and%
Difference between i ++ and ++ i
[Java] Introductory structure Class definition Relationship between class and instance Method definition format
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Rails] Difference between create method and new + save method
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between mockito-core and mockito-all
Difference between bundle and bundle install
Relationship between package and class
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
What is the difference between a class and a struct? ?? ??
Ethereum Transaction Sending Method Difference between send and sendAsync
Difference between Spring AOP and library proxy target class
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
[Ruby] Difference between get and post
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
Class method
Differences between Applet class and JApplet class
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
Let's override the difference between == (identity) and equals method (equivalence)
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Ruby] Maybe you don't really understand? [Difference between class and module]