-1

So i create a new class and i have a lot of objects in this class such as name, surname age etc. But i am geting the same error everytime. And also i do not now how to list my arrays with using method.

Error: no implicit conversion of Array into String

def main
patients = []
puts "What do you want to do \nadd \nlist \nexit"
process = gets.chomp
if process == "add"
    puts "Please enter patient's name"
    patient1 = Patient_Covid_19.new()
    patient1.Name = gets.chomp.to_s
    patient1.Name << patients #error line
elsif process == "list"
    #And i want to print the arrays(patients, ages, surnames etc.) in here but using a method. 
elsif process == "exit"
    puts "Have a nice day"
else
    puts "Please enter add, list or exit"
    main
end


end
main

Edit: It was small syntax mistake(error line). But i still need help for the list process.

Duke II
  • 15
  • 1
  • 6
  • Please edit your question and remove the part that was an error. Leaving it in the question is distracting and not pertinent to the question. Also, practice proper indentation. It helps avoid errors and makes it easier for others to see what you're doing. Finally, please see "[ask]", "[Stack Overflow question checklist](https://meta.stackoverflow.com/questions/260648)" and "[MCVE](https://stackoverflow.com/help/minimal-reproducible-example)" and all their linked pages. We need code that runs. – the Tin Man Apr 14 '20 at 06:42

2 Answers2

2

You probably meant to do patients << patient1.Name.

You can loop over and print out attributes as follows:

patients.each do |patient|
    puts "Name: #{patient.Name}, etc"
end
Kent Shikama
  • 3,910
  • 3
  • 22
  • 55
0
class Patient_Covid_19
 attr_accessor :Ssn, :Name, :Surname, :Sex, :Age
end

def main
patients = []
puts "What do you want to do \nadd \nlist \nexit"
process = gets.chomp
if process == "add"
    puts "Please enter patient's name"
    patient1 = Patient_Covid_19.new()
    patient1.Name = gets.chomp.to_s
    patients << patient1.Name
    main
elsif process == "list"

elsif process == "exit"
    puts "Have a nice day"
else
    puts "Please enter add, list or exit"
    main
end

end
main

This is my code. When the user writes Add, he will enter the patient's information from the console and this information will be added to an array. When the user writes list, he/she will be able to see the information of the patients he has written before. I want to do the listing with a method.

Duke II
  • 15
  • 1
  • 6