I’ve got a function that goes through an array of objects, and creates a new array of objects based on certain attributes from the original array. When I run this code I get the error
in ‘nonstop’: undefined local variable or method `sort_list’ for main:Object (NameError)
I made sure the sort_list array was initialized outside of the loop, and I’ve tried initializing it with a certain size too, but I keep getting this error. I’m pretty new to ruby, so what am I doing incorrectly?
def nonstop(flight_list)
index = 0
sort_list[] = nil
flight_list.each do |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 do |curr|
puts "#{curr.flightnum}\t#{curr.deptime}\t#{curr.arrtime}"
end
if (sort_list.empty?)
puts "none"
end
end
I think you need to initialize it like this:
This doesn’t throw an error in
irbfor me.