The value is taken out from the double hash and output by the getter.

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

The value is taken out from the double hash and output by the getter.
Get the value from the array and find out what number it is included in
How to pick up the input value by asynchronous communication and output the desired data from the server
Takes out the HashMap value and returns the result sorted by the value of value as a list.
[ruby] How to assign a value to a hash by referring to the value and key of another hash
Output the maximum value from the array using Java standard output
[Ruby] Difference between each method and for statement. The elements are taken out one by one and processed.
[Rails] [jQuery] Prevents the submit button from being pressed until the form is filled out and selected.
Check the contents by docker pull from ECR and start the container
How to output the value when there is an array in the array
Create a calendar from the Calendar class by specifying the year and month