Problem of visibility of variable ruby on rails



  • There's 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
    

    Shouting and fighting.

    undefined method `[] for nil:NilClass

    I'm changing.

    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
    

    What's going on?

    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
    



  • You have a mass. reservs_room size equal 2- Mm:

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

    a possible values j c 0 before 2*daysCount-1-sum_day_reserv*2Well, let's say it's your day. 16amount 8 and at j equal 2-м You get this:

    reservs_room[j]
    

    value nil




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2