Reputation: 1
I'm a relatively new Ruby student, and though I can tell it's never going to be a language I prefer I would like to at least understand it because its a gateway language. For my project we are instructed to solve a maze thats passed into a multi-dimensional array. We haven't learned recursion or any advanced techniques. This is an overview. My specific issue in my code is that in my while loop I am getting the error message for a value I'm passing into my array.
rb:60 in `: undefined method `[]' for nil:NilClass (NoMethodError) from -e:1:in `load'
I did research for an hour or so and it means that the value I'm passing into my array is nil, but I've assigned it a value.
My code is below. I've isolated it to the row
variable that is causing it to throw the error, but I have assigned row
a value, and that passes through fine. it's when I use row+=1
that it throws the error I think. (Though i could be incorrect.) I'm also asking my teacher about this but he hasn't responded so I thought I would post it here as well in hopes of finding a solution for the error.
maze=[]
def main_menu (options)
options.each_with_index do |option, index|
printf("%-s. %-s\n", "#{index+1}", "#{option}")
end
print "Please make a selection:"
main_selection=gets.to_i
return main_selection
end
menu_selection=["Maze 1", "Maze 2", "Maze 3", "Maze 4", "Maze 5", "Maze 6", "Maze 7", "Maze 8", "Maze 9", "Maze 10", "Quit"]
user_selection=0
path=1
row=0
column=0
while user_selection != menu_selection.length
user_selection=main_menu(menu_selection)
puts user_selection
case user_selection
when 1
maze.clear
mz_file=File.open("maze1.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
mz_file.close
maze.each do |row|
puts row.join
end
maze.each_index do |row|
column = maze[row].index "F"
if column
maze[row+1][column]=0
maze[row-1][column]=0
maze[row][column+1]=0
maze[row][column-1]=0
end
end
maze.each do |row|
puts row.join
end
#WHILE LOOP FOR MAZE
while row <=maze[0].length
column=0
while column <= maze.length
if maze[row][column]== 0
maze[row+1][column]==path
maze[row-1][column]==path
maze[row][column+1]==path
maze[row][column-1]==path
end
column=column+1
end
row=row+1
end
maze.each do |row|
puts row.join
end
when 2
maze.clear
mz_file=File.open("maze2.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
when 3
maze.clear
mz_file=File.open("maze3.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
when 4
maze.clear
mz_file=File.open("maze4.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
when 5
maze.clear
mz_file=File.open("maze5.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
when 6
maze.clear
mz_file=File.open("maze6.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
when 7
maze.clear
mz_file=File.open("maze7.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
when 8
maze.clear
mz_file=File.open("maze8.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
when 9
maze.clear
mz_file=File.open("maze9.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
when 10
maze.clear
mz_file=File.open("maze10.mz")
while !mz_file.eof?
line=mz_file.gets.chomp.split("")
maze.push line
end
maze.each do |row|
puts row.join
end
mz_file.close
end
end
Upvotes: 0
Views: 178
Reputation: 19855
You have a lot of problems in here. Your indentations are all wrong, which would help you track down some issues such as unclosed loops/blocks (which are an issue in the code you posted). You're shadowing your row
variable multiple times with loop indices, you're failing to strip white space before checking string responses (which is almost certainly putting some nil
s into your array), you are using ==
for what appear to be assignments around 50 lines into your program, you're using <=
to test an array index against a length.
Try testing your small tasks, such as reading a line of input and splitting it, in irb
. That gives you direct and immediate feedback if there's a problem with the way you're doing business. You should also consider using an editor or IDE that will help you with indentation and do some sanity checks on things like the ==
's where you appear to want assignment.
Upvotes: 4