Manasi Salvi

Understanding errors

A bit of background on this post - I am currently working on brushing up my Ruby programming knowlegde. I worked my way through the basics on Codeacademy. Codeacademy was a good way to get an intro but there is a fair bit of hand holding. So I moved on to the book ‘Learning Ruby the hard way’ by Zed A. Shaw. This book has been brilliant - there is a fair bit of hand holding initially but later on in the book as I found out you have to start building your own stuff.

This post is about my struggles with exercise 45 in the book- ‘You build a game’. I decided to build a pretty simple game incorporating the relevant components of the Ruby language and using the exercises from previous chapters as a bit og a guide. You can refer to my code here.

A continuous error I was getting whenever I ran this code was a no method error in Terminal:

 ex45-zaldo.rb:18:in `play': undefined method `next_room' for nil:NilClass (NoMethodError)

The code the line was associated with is:

class Hallway		 

	def initialize(room_map) 
		@room_map = room_map 
	end

	def play() 
		current_room = @room_map.opening_room()
		last_room = @room_map.next_room('finished')
		
	while current_room != last_room
		next_room_name = current_room.enter()
		current_room = @bone_map.next_room(next_room_name)
	end
	current_room.enter()
	end
end 

The main issue was I tried to copy some of the code from a previous exercise which I had difficulty with. So I decided to get a bit more practice I would follow the same format. What this mean’t was I ended up completely confusing myself as I didn’t work through the code from scratch and copied over a lot of classes and functions from the previous exercise. There were several naming errors which then led to errors like the one above. And I simply didn’t understand parts of the code.

For example in the code snippet above where it reads @bone_map should really read @room_map as I had literally just set the room_map instance variable in the initialize() function earlier.

This particular error took the longest to solve - the reason being I was reading the same piece of code over and over again and over time my brain was reading I wanted to see and not what was actually there and I was getting incredibly frustrated - in other words, I was fatigued and confused.

So I went back to the drawing board and hand wrote the flow of the game, rewrote the code with a more meaningful naming convention which then made it easier to connect the classes. I also made the storyline a lot simpler. There’s a desire to write an elaborate storyline which completely unnecessary as I realised. Sometimes there’s a benefit in keeping things simple so your code remains clean and easy to follow (for you).

The result was a short, simple game which did what the exercise had asked - and most importantly, it worked!


comments powered by Disqus