small medium large xlarge

Generic-user-small
06 Jan 2016, 03:36
Brian Davis (6 posts)

I’m working through ch03 on the Dijkstra algorithm. I’m getting:

undefined method `+' for nil:NilClass (NoMethodError)

at line 50 of cell.rb:

distances[linked] = distances[cell] + 1

I’ve double-checked my code against the downloaded source code, and I can’t see any difference. Any ideas why distances[cell] is nil here?

Generic-user-small
08 Jan 2016, 16:45
Jamis Buck (30 posts)

Very odd! I’ll take a look at what you’ve got and see if I can spot anything.

(edit: I originally asked to see the code – but missed that you’d liked to it already)

Generic-user-small
08 Jan 2016, 17:37
Jamis Buck (30 posts)

I cloned your repo and tried running the dijkstra.rb script, and I got an error (but not the one you indicated). I had to change this line:

https://github.com/brian-davis/mazes/blob/master/distances.rb#L5

So that it was

@cells[@root] = 0 # instead of @cell[@root]...

After that, the dijkstra.rb script worked fine. Was it a different script you were trying to run? If you’re still having the error, let me know and I’ll take another look.

Generic-user-small
10 Jan 2016, 20:01
Brian Davis (6 posts)

That was it. Thanks for helping me spot my typo.

You must be logged in to comment