initialization - Why is this local variable labeled as undefined? - Ruby -
i've got function goes through array of objects, , creates new array of objects based on attributes original array. when run code error
in 'nonstop': undefined local variable or method `sort_list' main:object (nameerror)
i made sure sort_list array initialized outside of loop, , i've tried initializing size too, keep getting error. i'm pretty new ruby, doing incorrectly?
def nonstop(flight_list) index = 0 sort_list[] = nil flight_list.each |curr| if (curr.depapt == argv[2] && curr.arrapt == argv[3]) sort_list[index] = curr index += 1 end end sort_list.sort! { |a,b| a.deptime <=> b.deptime} sort_list.each |curr| puts "#{curr.flightnum}\t#{curr.deptime}\t#{curr.arrtime}" end if (sort_list.empty?) puts "none" end end
i think need initialize this:
sort_list = []
this doesn't throw error in irb
me.
Comments
Post a Comment