There is a code

j = 0 10.times do |i| if @days[k].mday == reservs_room[j][0].mday then colspan = reservs_room[j][1]*2 j += 1 else colspan = 1 end end 

Screams and swears

undefined method `[] 'for nil: NilClass

Change to

 10.times do |i| if @days[k].mday == reservs_room[0][0].mday then colspan = reservs_room[0][1]*2 else colspan = 1 end end 

and all is well, what is the matter?

Full example:

 @days = Array.new(daysCount) i = 0; (-2..daysCount - 3).map {|n| @days[i] = Time.now + n.day; i += 1;} @reserv_cols = Array.new(@rooms.length) @rooms.each_index do |row_index| @reserv_cols[row_index] = [] k = 0 reservs_room = [[(Time.now), 3], [(Time.now+5.day), 5]] sum_day_reserv = 8 j = 0 num = 2*daysCount-1-sum_day_reserv*2 num.times do |i| if @days[k].mday == reservs_room[j][0].mday then colspan = reservs_room[j][1]*2 j += 1 else colspan = 1 end @reserv_cols[row_index].push([@days[k], colspan]) if i.odd? then k += 1 end end end 
  • An example cannot be verified. Can you give a minimal self-sufficient example that illustrates the problem? - D-side
  • one
    Most likely the problem is that reservs_room is less than 10 elements in the reservs_room array. - MAXOPKA
  • Maybe how to check if the array element is empty? - Pavel
  • In the full example, j is changing. An error occurs because of this. You can check the emptiness of the array with the same standard blank? present? any? . If you want your code to be readable and understandable - it is better to use the style guides . In this mess it was very difficult to make out something. - anoam
  • Why do railroad workers refuse to learn the language in which they write? any? never checked an array for emptiness. - Nakilon

1 answer 1

Your reservs_room array has a size of the 2 :

 reservs_room = [[(Time.now), 3], [(Time.now+5.day), 5]] 

and the possible values ​​for j from 0 to 2*daysCount-1-sum_day_reserv*2 , well, let's say you have 16 days, the sum is 8 and if j 2-м you get here:

 reservs_room[j] 

nil value