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
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.
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.)
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.
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.
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