class Fruits
def initialize(name, color, variety) #Formal argument (first argument,Second argument,Third argument)#The initialize method does not require a new call.
@name = name #Define instance variables
@color = color
@variety = variety
end
def name #Getter(Getterメソッド名は、クラス外で出力する際に用いますので、重要です。)
@name #Getting the value of the instance
end
def color #Also getter
@color
end
def variety
@variety
end
end
class InfoFruits #I need a call.
def initialize(fruits) #Definition of instance variables
@fruits = fruits
end
def fruits #Getter
@fruits #Get the value of an instance variable#An array of three actual arguments.
end
def info_fruits
self.fruits.each do |fruit| #Each method iterates as many times as there are elements in the array.
#self.fruits is an array of instance values and three actual arguments.
puts "#{fruit.name}Is#{fruit.color}is.品種Is#{fruit.variety}is." #ゲッターで取得しました値の出力の際Is**Instance name.Getter method name**I will describe it as.
end
end
end
array = [{info:{info_fruit_name: "Apple", info_fruit_color: "red"}}, #The array contains a double hash.
{info:{info_fruit_name: "banana", info_fruit_color: "yellow"}},
{info:{info_fruit_name: "Grape", info_fruit_color: "purple"}}]
fruits = []
array.each do |double_hash| #Each method iterates as many times as there are elements in the array.
fruit_name = double_hash[:info][:info_fruit_name] #In the case of double hash, you can get the desired value by selecting two keys.
fruit_color = double_hash[:info][:info_fruit_color]
puts "#{fruit_name}Which variety is it?"
double_hash[:info][:info_fruit_variety] = gets.chomp #By specifying a new key in the hash, it is possible to add a new value.
fruit_variety = double_hash[:info][:info_fruit_variety]
if fruit_variety == "Shine Muscat" #Conditional branch
fruit_color = "green"
end
fruits << Fruits.new(fruit_name, fruit_color, fruit_variety) #Pass the actual argument to the Fruits class, create an instance, and at the same time, the initialize method processes it, defines the instance variable, and gets the value by the getter.
#Match the number of formal and actual arguments.#By getting the value, it is possible to output outside the scope of the class. When outputting**Instance name.Getter method name**I will describe it as.
end
info_fruit = InfoFruits.new(fruits) #Pass the array fruits with three actual arguments to the InfoFruits class, create an instance, the initialize method will process it, define the instance variable, and get the value by the getter.
info_fruit.info_fruits #info_A call to the fruits instance method.
#Output result
Which apple variety is it?
Fuji
Which is the banana variety?
Giant Cavendish
Which grape variety is it?
Kyoho
The apples are red. The variety is Fuji.
Bananas are yellow. The variety is Giant Cavendish.
The grapes are purple. The variety is Kyoho.
#Output result
Which apple variety is it?
Fuji
Which is the banana variety?
Giant Cavendish
Which grape variety is it?
Shine Muscat
The apples are red. The variety is Fuji.
Bananas are yellow. The variety is Giant Cavendish.
The grapes are green. The variety is Shine Muscat.
We would appreciate it if you could point out any mistakes or lack of recognition.
Recommended Posts