我正在尝试使用数组中的对象变量来与用户输入进行比较,以查看他们本月是否有约会。运行时,我在a [i]之后的月份得到一个未定义的方法错误。无论如何都要调用我列出的属性吗?
class Month < Appointment
attr_accessor :des, :day, :month, :year
class << self
def occurOn2(a,uDay, uMonth, uYear)
appts = 0
for i in (0..10) do
if a[i].month == uMonth
puts "You're #{a[i].des} is scheduled for this month #{a[i].month} "
appts =+ 1
end
end
if appts < 1
puts "You do not have any appointments at this time."
end
end
end
end
答案 0 :(得分:0)
你的代码可以使用重构(10看起来像一个'神奇的数字'),但我认为你说的这个错误可以通过简单的零检查解决。
for i in (0..10) do
if a[1] # or "if defined?(a[1].month)" if you want to be more specific
if a[i].month == uMonth
puts "You're #{a[i].des} is scheduled for this month #{a[i].month} "
appts =+ 1
end
end
end