arrays - 我想用 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
编辑:这是一个小的语法错误(错误行)。但我仍然需要列表过程的帮助。
解决方案
你可能打算这样做patients << patient1.Name
。
您可以循环并打印出属性,如下所示:
patients.each do |patient|
puts "Name: #{patient.Name}, etc"
end
推荐阅读
- vba - 如何使用 VBA 将记录从 rs1 移动到 rs2 (ADODB.Recordsets)?
- javascript - 为什么 fetch 函数说我必须使用绝对 url,即使我设置了代理?
- r - 在多个栅格上绘制多个 shapefile
- c# - 如何在访问数据库与 2 个日期之间进行搜索?
- docker - Alpine apk add --repository 升级其他包
- windows - 如何在 Windows 上使用 CMake 使用 find_package 查找 Google 测试?
- electron - 当我的 Electron 应用程序在 macOS 上启动时,如何避免显示 Dock 图标?
- mercurial - 在 Mercurial 中复制一个分支?
- python - 图片未保存在媒体文件夹中
- scripting - 为脚本开发记录 GIMP 2.10 命令?