将对象推送到约会数组

时间:2017-02-17 21:14:19

标签: arrays ruby push

我必须从appt2中输入信息,并将其推送到我的约会数组中。

到目前为止我所拥有的:

appointments = []
appt2 = MonthlyAppointment.new("Doctor", "Cold", 2, 30, 15)
appointments.push(appt2)

我可以修复什么?

1 个答案:

答案 0 :(得分:0)

如果您只做一件事,可以删除中间变量:

appointments = [
  MonthlyAppointment.new(...)
]

您可以使用<<的{​​{1}}别名:

push

你所拥有的是相当典型的Ruby代码。