# Catching (or saving) errors

**URL:** <https://www.ruby-forum.com/t/catching-or-saving-errors/145014>\
**Category:** Ruby\
**Created:** [August 5, 2008, 7:35am UTC](https://www.ruby-forum.com/t/catching-or-saving-errors/145014 "2008-08-05T07:35:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [August 5, 2008, 7:35am UTC](https://www.ruby-forum.com/t/catching-or-saving-errors/145014/1 "2008-08-05T07:35:34Z")

</div>

So, I am currently working on an Interactive Fiction like game in Ruby,  
and have stumbled across a bit of a problem. The ‘move’ function makes a  
call to the room’s ‘exits’ variable (exits[:north], for instance) but I  
want it to catch if the variable :north doesn’t exist to display  
something like “You can’t go taht way!”. I can’t figure out how to do  
this with my meager skills. ☹

Any help would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [August 5, 2008, 8:11am UTC](https://www.ruby-forum.com/t/catching-or-saving-errors/145014/2 "2008-08-05T08:11:32Z")

</div>

On Mon, Aug 4, 2008 at 10:34 PM, Tim M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So, I am currently working on an Interactive Fiction like game in Ruby,  
> and have stumbled across a bit of a problem. The ‘move’ function makes a  
> call to the room’s ‘exits’ variable (exits[:north], for instance) but I  
> want it to catch if the variable :north doesn’t exist to display  
> something like “You can’t go taht way!”. I can’t figure out how to do  
> this with my meager skills. ☹
> 
> Any help would be greatly appreciated!

This should illustrate the basic approach:

exits = {:north =\> ‘front of building’, :east =\> ‘fork in road’}  
print "what now? \> "  
move = gets.chomp.downcase  
dir = case move  
when “n”, “north”  
:north  
when “s”, “south”  
:south  
when “e”, “east”  
:east  
when “w”, “west”  
:west  
else  
nil  
end

if dir  
if exits[dir]  
puts “moving to #{exits[dir]}”  
else  
puts “you can’t go that way!”  
end  
else  
puts “no such direction. please enter one of [n]orth, [s]outh, [e]ast  
or [w]est”  
end

martin

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [August 5, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/catching-or-saving-errors/145014/3 "2008-08-05T16:30:12Z")

</div>

Didn’t know a nice if like that would just take care of it! Also, did  
you test that code? Or the concept? I’m pretty sure you can’t do  
exits[avar[. I did some testing with that earlier…

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [August 5, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/catching-or-saving-errors/145014/4 "2008-08-05T18:13:05Z")

</div>

Thanks a ton! You’ve helped out a lot! Oh, and btw, I still think the  
whole obj\_sword.strike(:martin) bit is hilarious 😛

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [August 5, 2008, 4:48pm UTC](https://www.ruby-forum.com/t/catching-or-saving-errors/145014/5 "2008-08-05T16:48:55Z")

</div>

On Tue, Aug 5, 2008 at 7:28 AM, Tim M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Didn’t know a nice if like that would just take care of it! Also, did  
> you test that code? Or the concept? I’m pretty sure you can’t do  
> exits[avar[. I did some testing with that earlier…

Yep, I tested it before pasting it in. What I’m doing is this:

1. defining exits as a hash
2. writing a case statement to convert move from “N” or “n” or “north”  
or “NORTH” to :north
3. using the converted move as a key to look up the exits hash

In the context of a larger game, you’d have a $rooms hash, with unique  
identifiers like “front of building” as location keys. So you’d say

if dir  
if exits[dir]  
player.move\_to(exits[dir])  
end  
end

where player.move\_to(location) would be defined something like

def move\_to(location)  
loc = $rooms[location]  
if loc  
self.current\_location = loc  
end  
end

martin
