[RUBY] I compared classes and instances with worldly things

I compared classes and instances with worldly things

Even I, a beginner, can understand that it is inseparable even if I cut class or instance in ruby. (I'm only studying ruby, so I don't know anything else) But even if I can understand it in sentences, I can't write it when I try to make it into code, so I don't think I understand it. Therefore, I would like to compare it with a worldly one so that I can easily understand it.

What are classes and instances in the first place?

A class is like a blueprint and has no content in itself. instance is an entity or data created based on that class. Can be generated with class name.new

A class method is a method that defines an action that returns a common result regardless of all the individual information created from the class. Instance method is a method that can be set separately for each attribute of the instance. By using instance variables, attribute values ​​set separately for each instance can be set.

class class
  def self.Method name#Class method
processing
  end

def method name#Instance method
    @Variable name=value#Instance variables
processing
  end
end

name of the class.new #Instance generation
Instance name.Method name#Instance method call
name of the class.Method name#Calling a class method

Perhaps most of the teaching materials are written like this? I understand what you mean! So that's it! What is it! But if you look at the sample code after that? It will be. In such a state, it's an initialize method, and even if I was told that instance variables could be used in other methods, it would be more confusing !!

When it comes to code, it suddenly becomes difficult to stick to, so I would like to compare it with a broken expression.

From a certain anime

Well, I think there are many ways to compare it, but I would like to compare it with the trendy XX blade. I thought it would be okay to use Pomon! I couldn't find a common process! !!

Classes and instances

I would like to write the code first and explain it one by one.

class Kisatutai 

end

tannzirou = Kisatutai.new
zennitu = Kisatutai.new

Well, it looks like this.

First of all, regarding the class, the blueprint is a common one, and what does the group represent? This class is for demon slaughterers. So who is the demon slaughterer? Someone called that will be an instance this time. The guests are Mr. Sumijiro and Mr. Zeni.

About instance methods (+ attributes and attribute values)

So, first you have to enter the person's information.

This time, we will group by the name`` color`` breathing type` of the demon slaughter corps. The information that must be entered for each person becomes the ** attribute **, and the information that the person actually has becomes the ** attribute value **. Both Sumijiro-kun and Zeni-kun belong to the same class called Oni-Kaitai, but each sword and breathing is different, so it is necessary to enter information. Instance methods define what to do individually.

class Kisatutai
  def profile(namae, iro, zokusei) #Attribute is specified for the class (name, sword color, breathing type)
    puts "name:#{namae}"
    puts "Sword color:#{iro}color"
    kokyuu = "#{zokusei}Breathing"
    puts "Breathing used:#{kokyuu}"
  end
end

tannzirou = Kisatutai.new
tannzirou.profile("Tanjiro Kamado", "black", "water") # ()The content is an attribute value
zennitu = Kisatutai.new
zennitu.profile("Zenitsu Agatsuma", "yellow", "Thunder")

=>Output result
Name: Tanjiro Kamado
Sword color: black
Breathing used: Breathing water
Name: Yoshiyasu Gazuma
Sword color: yellow
Breathing used: Lightning breathing

This is because the profile method specifies that this attribute should be entered, so the attribute value is entered in tannzirou.profile. With this, I was able to enter detailed information about the two of them.

About class methods

On the other hand, the class method is a process that can be used by all the demon slaughterers, regardless of whether they are charcoal or good. So It will be the Kisatutai. Method.

class Kisatutai 
  def self.zennsyuutyuu #Class method Anyone can use it (really training is required during full concentration)
    puts "Full concentration"
  end
end
Kisatutai.zennsyuutyuu #Processing using common information in the class

=>Output result
Full concentration

Take a breath

So far, the processing up to this point is like this.

class Kisatutai

  def profile(namae, iro, zokusei) #Attribute is specified for the class (name, sword color, breathing type)
    puts "name:#{namae}"
    puts "Sword color:#{iro}color"
    kokyuu = "#{zokusei}Breathing"
    puts "Breathing used:#{kokyuu}"
  end

  def self.zennsyuutyuu #Class method Anyone can use it (really training is required during full concentration)
    puts "Full concentration"
  end
end

tannzirou = Kisatutai.new
tannzirou.profile("Tanjiro Kamado", "black", "water") # ()The content is an attribute value
Kisatutai.zennsyuutyuu #Processing using common information in the class
puts "-------------------------------"
zennitu = Kisatutai.new
zennitu.profile("Zenitsu Agatsuma", "yellow", "Thunder")
Kisatutai.zennsyuutyuu #Processing using common information in the class

At this point, if you like, copy and paste and run ruby, I think it will be easier to get an image.

About instance variables and initialize method

Write a piece of code before the instance variable

class Kisatutai
  def itinokata(wazamei)
    puts "Ichi type#{wazamei}" 
  end
end

tannzirou = Kisatutai.new
tannzirou.itinokata("Water cutting") 
Kisatutai.zennsyuutyuu
zennitu.itinokata("A flash of haze")

=>Output result
Ichi type water surface cutting
Ichi's pattern, Kasumi Ippen

As you all know, in the case of anime, it's really "water breathing, Ichi-no-kata water surface cutting", isn't it? If you want to add breath to this itinokata method, you can add attributes and attribute values. Did you use breathing in your profile? The characteristic of instance variables is that variables defined by other methods can be used in other places. Therefore, you can reuse it by adding the breathing part of the profile as an instance variable.

  def profile(namae, iro, zokusei) 
    puts "name:#{namae}"
    puts "Sword color:#{iro}color"
    @kokyuu = "#{zokusei}Breathing" #@koyuu is an instance variable
    puts "Breathing used:#{@kokyuu}"
  end

  def itinokata(wazamei)
    puts "#{@kokyuu}Ichi type#{wazamei}" 
  end

It looks like this. So, shouldn't the profile appear first? Take the trouble

tannzirou = Kisatutai.new
tannzirou.profile() 

I think it's annoying to write. Anyway, do it ... In such a case, the initialize method will be performed at the same time as the instance is created. Therefore, rewrite the profile to initialize.

  def initilize(namae, iro, zokusei) 
Omission
  end

This will run your profile from the beginning.

Summary

To summarize the above, it looks like this.


class Kisatutai #Class blueprint (group this time)

  def initialize(namae, iro, zokusei) #A method that will do whatever you want when you create a new class
    #Attribute is specified for the class (name, sword color, breathing type)
    puts "Member profile"
    puts "name:#{namae}"
    puts "Sword color:#{iro}color"
    @kokyuu = "#{zokusei}Breathing" #@koyuu is an instance variable
    puts "Breathing used:#{@kokyuu}"
  end

  def self.zennsyuutyuu #Class method Anyone can use it (really training is required during full concentration)...)
    puts "Full concentration"
  end

  def itinokata(wazamei) #Instance method Ichi type Depends on the skill
    puts "#{@kokyuu}Ichi type#{wazamei}!!!!!!" 
    #Instance variable (@kokyuu) can be used anywhere in the class instance method
  end
end

tannzirou = Kisatutai.new("Tanjiro Kamado", "black", "water") #Instance creation (with attribute value)
Kisatutai.zennsyuutyuu #Processing using common information in the class
tannzirou.itinokata("Water cutting") #Processing using individual information (attribute value)
puts "------------------------------"
zennitu = Kisatutai.new("Zenitsu Agatsuma", "yellow", "Thunder")
Kisatutai.zennsyuutyuu
zennitu.itinokata("A flash of haze")

=>Output result
Member profile
Name: Tanjiro Kamado
Sword color: black
Breathing used: Breathing water
Full concentration
Breathing of water Ichi type water surface cutting!!!!!!
------------------------------
Member profile
Name: Yoshiyasu Gazuma
Sword color: yellow
Breathing used: Lightning breathing
Full concentration
Breathing of thunder Ichi type Kasumi Ippen!!!!!!
------------------------------

I hope you can get an image of it. I dare to use romaji for variable names, but I have to use English that is really easy to understand (laughs). If it helps you study If you have any suggestions, please contact us. Thank you for reading this far! !!

Serpentine

I made this code work. I would be very happy if you could copy and play with me.

class Kisatutai #Class blueprint (group this time)

  def initialize(namae, iro, zokusei) #A method that will do whatever you want when you create a new class
    #Attribute is specified for the class (name, sword color, breathing type)
    puts "Member profile"
    puts "name:#{namae}"
    puts "Sword color:#{iro}color"
    @kokyuu = "#{zokusei}Breathing" #@koyuu is an instance variable
    puts "Breathing used:#{@kokyuu}"
  end

  def self.zennsyuutyuu #Class method Anyone can use it (really training is required during full concentration)
    puts "Full concentration"
  end

  def itinokata(wazamei) #Instance method Ichi type Depends on the skill
    puts "#{@kokyuu}Ichi type#{wazamei}!!!!!!" 
    #Instance variable (@kokyuu) can be used anywhere in the class instance method
  end
end

puts "What is your favorite character of Devil?"
namae = gets.chomp
puts "Please enter in romaji"
name = gets.chomp
puts "What is the color of that person's sword?"
iro = gets.chomp
puts "What kind of breathing does that person have?"
zokusei = gets.chomp
puts "Please tell me the type of Ichi that the person uses"
wazamei = gets.chomp

puts "------------------------------"

name = Kisatutai.new(namae, iro, zokusei)
Kisatutai.zennsyuutyuu
name.itinokata(wazamei)

Recommended Posts

I compared classes and instances with worldly things
Writing code with classes and instances
Classes and instances
Getting Started with Java_Chapter 8_About Instances and Classes
[Ruby] Classes and instances
About classes and instances
Ruby classes and instances
java (classes and instances)
About classes and instances (evolution)
Consideration about classes and instances
About Ruby classes and instances
Creating Ruby classes and instances
I compared PHP and Java constructors
Organize classes, instances, and instance variables
Classes and instances Java for beginners
I compared Java and Ruby's FizzBuzz.
Java programming (classes and instances, main methods)
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Write code using Ruby classes and instances
I implemented Ruby with Ruby (and C) (I played with builtin)
I also tried WebAssembly with Nim and C
I compared the characteristics of Java and .NET
I tried to read and output CSV with Outsystems
About the difference between classes and instances in Ruby
I started MySQL 5.7 with docker-compose and tried to connect
[Ruby] I made a crawler with anemone and nokogiri.
Java classes and instances to understand in the figure
I want to transition screens with kotlin and java!
Regarding the difference between Java array and ArrayList, I compared and corresponded methods with similar functions.