Easy to understand the difference between Ruby instance method and class method.

Introduction

Until now, I have not clearly understood the important concepts of Ruby, such as instances and classes, so I will explain them with examples. I think it's something you absolutely must know to use Ruby, but it wasn't easy to understand.

Subject of this article

・ For those who are new to Ruby ・ People whose classes and instances are ambiguous

What is an instance class?

As an example, let's assume that users are posting with ids 1 to 3 as shown below under the Tweet class in a service like twitter. Tweet.png

At this time, each post enclosed in a black square is a ** instance **. Instances are said to be objects created from the types of classes. And Tweet is ** class **. An image of an instance belonging to a class. It's an important concept in Ruby! !!

Understand in code

class Tweet
  #Class method
  def self.tweet
    puts "tweet"
  end

  #Instance method
  def post_tweet(user,about)
    puts "#{user}Is#{about}Posted about"
  end
end

#Run
Tweet.tweet

new_tweet = Tweet.new()
new_tweet.post_tweet("choco","programming")

After execution

tweet
choco posted about programming

Class method => Method performed on the class.

-Call with "class name.method" outside class. (Tweet.tweet in the above example) ・ If you cut yourself, you will get angry. (** declare itself as a class method **) => It can be defined by ** self. method name ~~ end ** in the class. -All, new, etc. are predefined class methods, and you can operate on the class. (All gets all instances of the class, new creates a new instance in the class)

Instance method => Method performed on the instance.

-Call with "instance name.method" outside the class. (In the above example, new_tweet.post_tweet) Since the instance belongs to the class, create an instance with ** class name.new ** and execute the method for that instance. -Basically, I feel that instance methods are often used during implementation.

end

As I wrote this article, I thought that Ruby was a very easy-to-understand and intuitive design. From now on, I will try to define the method with a strong awareness of the instance class.

Thank you to everyone who read it. If you have any suggestions, please do not hesitate to contact us.

Recommended Posts

Easy to understand the difference between Ruby instance method and class method.
Difference between instance method and class method
Difference between class and instance
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Difference between Ruby instance variable and local variable
[Ruby] Maybe you don't really understand? [Difference between class and module]
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
[Java] Understand the difference between List and Set
[iOS] Understand the difference between frame and bounds
Understand the difference between abstract classes and interfaces!
Ruby: Differences between class methods and instance methods, class variables and instance variables
[Ruby] I thought about the difference between each_with_index and each.with_index
What is the difference between a class and a struct? ?? ??
[Ruby] Method to easily get the receiver type .class
About the difference between classes and instances in Ruby
Difference between variables and instance variables
[Ruby] How to calculate the total amount using the initialize method and class variables
[Ruby] What is the slice method? Let's solve the example and understand the difference from slice!
Difference between Java and JavaScript (how to find the average)
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
Let's override the difference between == (identity) and equals method (equivalence)
The difference between programming with Ruby classes and programming without it
[Ruby] Difference between get and post
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Note: Difference between Ruby "p" and "puts"
[Ruby] From the basics to the inject method
[Java] Differences between instance variables and class variables
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
[Easy] How to upgrade Ruby and bundler
[Rails] I studied the difference between new method, save method, build method and create method.
Understand the difference between int and Integer and BigInteger in java and float and double
I don't really understand the difference between swift Error and NSError, so I tried to summarize it myself.
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
[Ruby] Difference between puts and return, output and return value
[Rails] Difference between create method and new + save method
Ruby How to convert between uppercase and lowercase
[Ruby basics] How to use the slice method
[Promotion of Ruby comprehension (1)] When switching from Java to Ruby, first understand the difference.
[Rails] I learned about the difference between resources and resources
What is the difference between System Spec and Feature Spec?
Ethereum Transaction Sending Method Difference between send and sendAsync
Bind the request to any class and receive it
Calculate the difference between numbers in a Ruby array
[Rails] What is the difference between redirect and render?
Compare the difference between dockerfile before and after docker-slim
[JAVA] What is the difference between interface and abstract? ?? ??
Difference between Spring AOP and library proxy target class
Java classes and instances to understand in the figure
Understand in 3 minutes! A very rough explanation of the difference between session and cookie
What is the difference between skip and pending? [RSpec]
How to create and execute method, Proc, Method class objects
[Ruby] Class nesting, inheritance, and the basics of self