Blog

Exercise 35: Branches and Functions: Learn Ruby the Hard Way: Practicum (TESTHEAD)

On December 8, 2011, in Syndicated, by Association for Software Testing
0

So far, we have looked at decisions, and we have looked at functions. We have even looked at functions that interact with our main program as we pass values into a function.  Now we’re going to go a little crazy and put it all into a blender. What do I mean by that?

We’re going to take the decision tree options and integrate them into functions. What’s more, we’re going t set up our functions so that we are calling other functions and getting their output to send as the input to other functions. It sounds dizzying doesn’t it? Well, it can be, but if we take the time to consider it directly, it’s not so bad.

Here’s the code example we’re given to work with:

What You Should See

Here’s Zed’s output of him playing the game:

$ ruby ex35.rb
You are in a dark room.
There is a door to your right and left.
Which one do you take?
> left
There is a bear here.
The bear has a bunch of honey.
The fat bear is in front of another door.
How are you going to move the bear?
> taunt bear
The bear has moved from the door. You can go through it now.
> open door
This room is full of gold.  How much do you take?
> asf
Man, learn to type a number. Good job!
$

And here’s my output.

Extra Credit

Draw a map of the game and how you flow through it.

Fix all of your mistakes, including spelling mistakes.

[ Think I covered this while going through it]

Write comments for the functions you do not understand. Remember RDoc comments?

[ Amazingly, I think I’ve got this 🙂 ]

Add more to the game. What can you do to both simplify and expand it. The gold_room has a weird way of getting you to type a number. What are all the bugs in this way of doing it? Can you make it better than just checking if “1” or “0” are in the number? Look at how to_i() works for clues.

[ I tried a number of different ways to get the var.include? option to work with a range or a regex value, but they were not successful. I did recall reading up on options using ‘=~’, so that’s what I decided to use. See below. Note: though it’s an improvement, it doesn’t quite cover all areas (still looking for a way to disallow values that combine letters and numbers, but this works for any purely numeric values and purely alphanumeric values ]

TESTHEAD’s TAKEAWAYS:

That was pretty cool. It’s interesting to see that, once a function is defined, it really doesn’t matter the order they are defined or that they have function calls within function calls, and that they can be deeply nested and able to call other functions anywhere in the the process. It’s like a web and the ability to follow any line to effectively get to any other function. that’s interesting. Working through how to get the numbers to be recognized was a bit of a struggle, I tried quite a few things until I had success with the method I used. I’m sure there are other ways, but this suits the purpose for this example.

 

Comments are closed.


Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!