[RUBY] Classes and instances

What is a class?

It's like a blueprint or a cooking recipe. It takes time and effort to make a new product from scratch each time. In order to avoid it, it is possible to create individual data while observing common rules. Classes only determine common processing and attributes, and cannot handle data by themselves.

class class name
  #Definition of variables and methods
end

This time, let's define a Jam class that creates jam.

class Jam

end

What is an instance?

It is the "thing" that is actually made. I have defined a class, but the class alone does not have any data. Create an instance with data from the class. Even if the class does not define it, it has a method called new from the beginning and by executing this An instance with the data is created.

To be able to add data and execute methods after instantiating Create by assigning to a variable.

Variable name=name of the class.new

When you actually create an instance

class Jam

end

strawberry_jum = Jam.new

It's empty now because nothing is written in the recipe.

Instance method

As the name suggests, describe it in the class with a method that can be used by the instance.

class class name
def method name
    #processing
  end
end

Let's actually define an instance method.

class Jam
  def make_jam
   #Fill in the process
  end
end

strawberry_jum = Jam.new
strawberry_jum.make_jam

Nothing happens because I haven't defined the contents of the method yet. (I will fill in the contents later and use it.)

Instance variables

It defines the attributes of the data. By defining it, all instances have the same attributes, You can set different values ​​for each. For example, even if you define the same attribute of "ball", there are various values ​​such as "soccer ball", "basketball", and "baseball ball".

Define it like this by prefixing the variable with @.

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

The scope of an instance variable is all instance methods of that class, so it can be used for all instance operations.

initialize method

An instance method that executes the defined process as soon as the instance is created. In this case, it is generated at the same time when the instance is created by the new method.

Let's use instance variables and the initialize method.

class Jam
  def initialize(jam_taste,jam_color,jam_price) #The passed argument is assigned
    @taste = jam_taste    #jam_Strawberry is assigned to taste
    @color = jam_color    #jam_red is assigned to color
    @price = jam_price    #jam_300 is assigned to price
  end

  def make_jam
  end
end

strawberry_jum = Jam.new('Strawberry','Red',300)#Pass arguments
strawberry_jum.make_jam

Let's describe the process in the instance method make_jam and use it.

class Jam
  def initialize(jam_taste,jam_color,jam_price) 
    @taste = jam_taste 
    @color = jam_color   
    @price = jam_price    
  end

  def make_jam
    puts "#{@taste}Make a taste jam. color is#{@color}The price is by color{@price}It's a yen"
  end

    strawberry_jum = Jam.new('Strawberry','Red',300)
    strawberry_jum.make_jam  #Make strawberry-flavored jam. The color is red and the price is 300 yen.
end

By the way, what to do if you want to make tangerine-flavored jam

class Jam
  def initialize(jam_taste,jam_color,jam_price) 
    @taste = jam_taste 
    @color = jam_color   
    @price = jam_price    
  end

  def make_jam
    puts "#{@taste}Make a taste jam. color is#{@color}The price is by color{@price}It's a yen"
  end

    strawberry_jum = Jam.new('Strawberry','Red',300)
    strawberry_jum.make_jam  
    
    orange_jum = Jam.new('Mandarin orange','Orange','250')
    orange_jum.make_jum #Make tangerine-flavored jam. The color is orange and the price is 250 yen.

end

It looks like this.

Class method

It is used when you want to define common processing in the class. Define the method name by prefixing it with self

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

Let's actually use it.

class Jam
  def self.use
   puts "Use by spreading on bread"
  end

  def initialize(jam_taste,jam_color,jam_price) 
    @taste = jam_taste 
    @color = jam_color   
    @price = jam_price    
  end

  def make_jam
    puts "#{@taste}Make a taste jam. color is#{@color}The price is by color{@price}It's a yen"
  end

    strawberry_jum = Jam.new('Strawberry','Red',300)
    strawberry_jum.make_jam  
    
    orange_jum = Jam.new('Mandarin orange','Orange','250')
    orange_jum.make_jum #Make tangerine-flavored jam. The color is orange and the price is 250 yen.
   
    Jam.use #It is output that it is used by spreading it on bread.
    
end

that's all.

Recommended Posts

Classes and instances
[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
Writing code with classes and instances
Organize classes, instances, and instance variables
Classes and instances Java for beginners
Java programming (classes and instances, main methods)
Memorandum (Ruby: Basic Grammar: Classes and Instances)
Write code using Ruby classes and instances
java classes, instances, objects
HashMap and HashSet classes
String literals and instances
List and happy classes
Getting Started with Java_Chapter 8_About Instances and Classes
I compared classes and instances with worldly things
[Java] Generics classes and generics methods
About the difference between classes and instances in Ruby
Java classes and instances to understand in the figure
[Ruby] Singular methods and singular classes
Ruby methods and classes (basic)
Java abstract methods and classes
[Ruby] Singular methods and singular classes
[For beginners] Explanation of classes, instances, and statics in Java
[Ruby] Creating code using the concept of classes and instances
[Ruby] Classes, instance variables, instances, etc ...
How to call classes and methods
Java generics (defines classes and methods)
== and equals
Mock and test Autowired classes (MockitoExtension, initMocks)
Azure Container Instances! Lightweight and explosive container usage!
JAVA learning history abstract classes and methods
Comparison of JavaScript objects and Ruby classes
[Details] Let's master abstract classes and interfaces! !!
[Java ~ Classes and External Libraries ~] Study Memo (6)
Effective Java 3rd Edition Chapter 4 Classes and Interfaces
[Java] Personal summary of classes and methods (basic)
Understand the difference between abstract classes and interfaces!
Differences in writing Java, C # and Javascript classes
Kantai Collection Java # 1 Classes and Objects [For Beginners]