首页 > 解决方案 > 我想用 using 方法列出“用户输入”

问题描述

所以我创建了一个新类,我在这个类中有很多对象,例如姓名、姓氏年龄等。但我每次都得到同样的错误。而且我现在不知道如何使用 using 方法列出我的数组。

错误:没有将数组隐式转换为字符串

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

编辑:这是一个小的语法错误(错误行)。但我仍然需要列表过程的帮助。

标签: arraysruby-on-railsrubyfunctionoop

解决方案


你可能打算这样做patients << patient1.Name

您可以循环并打印出属性,如下所示:

patients.each do |patient|
    puts "Name: #{patient.Name}, etc"
end

推荐阅读