Mike
Mike

Reputation: 59

Error accessing data from an array in ruby ​on rails

The error tells me that the method '[]' is not defined in the line 127
this is my code and Idon't see the error

def verificar_area archivo
bandera= false
Spreadsheet.client_encoding = 'UTF-8'
book = Spreadsheet.open archivo
@wk = book.worksheet 0

areas = ["Secundaria",
"Preparatoria",
"Universitario",
"Edad 11 a 15 años",
"Edad de 16 a 19 años",
"Solo estudian",
"Adolescentes que trabajan y estudian"]

(1 .. @wk.row_count).each do |index_renglon|
  @renglon_excel = @wk.rows[index_renglon]
  area=@renglon_excel[8]                        #This is the line 127
    if areas.include?(area)
      bandera=true
    else
      bandera=false
      break
    end #fin if
  end #fin for
  return bandera
end

the error in the browser is this this is the line 127 => area=@renglon_excel[8]

Upvotes: 1

Views: 60

Answers (1)

Deepak Mahakale
Deepak Mahakale

Reputation: 23671

You are getting the error because @renglon_excel is nil for that specific iteration

@renglon_excel[8]

And you are trying to access with index of [8] on nil

To avoid this you can add a check

@renglon_excel = @wk.rows[index_renglon]
next unless @renglon_excel
area = @renglon_excel[8]

Upvotes: 1

Related Questions