Learn Ruby the Hard Way – Exercise 49 Make a Sentence

Here is my work for Exercise 49.
I added a parser to the code from Exercise 48: parser.rb under lib/ex48
Then I wrote tests for the parser: test_parser.rb file under tests
I ran into a bit of trouble when I tried to test the skip function. Any tests that used the skip function would be skipped. Thinking it could be the word skip causing problems, I changed the function name to skipper, and the tests did not skip. Hmm, ok. I then changed the function name back to skip, but put all the methods inside a class Parser. The tests did not skip. It seemed the word skip was a key word and causing the Test::Unit tests to be skipped.
I upgraded my Ruby version from 2.0.0 to 2.2.1 and also upgraded my test-unit gem. The tests stopped skipping, even with the word skip used as a function name. Whaaat. I ended up keeping all the methods in a Parser class anyways, but this could be something people with just the functions would run into.
Study Drills:
1. Change the parse_ methods and try to put them into a class rather than be just methods. Which design do you like better?
I prefer having all the methods into a class Parser.
2. Make the parser more error-resistant so that you can avoid annoying your users if they type words your lexicon doesn’t understand.
If words are not in the lexicon, we can assign it to [‘uncategorized’, ‘word’]. Then when we use our parser, if the word is uncategorized but where we expect a verb to be for example, we can assign it as the verb of the sentence.

3. Improve the grammar by handling more things like numbers.
I added a parse_number() that looks for a number before the object. If no number is present, a number of 1 is assigned.

4. Think about how you might use this Sentence class in your game to do more fun things with a user’s input.
Users will be able to enter more fluid input and not be trapped! They will be able to pick up different objects, and go in different directions among other exciting new developments.
Summary
In this exercise, I learned how to create a parser to compliment our lexicon scanner in Exercise 48. The parser has functions that skip words and and parse words. Using the parser, we can parse a list of sorted words (for example [[‘noun’, ‘bear’], [‘verb’, ‘drink’], [‘noun’, ‘coffee’]]) into a sentence object. The sentence object will have a subject, a verb, and an object variable. Using our parser on the example above will give us a sentence object that has a @noun variable of “bear”, a @verb variable of “drink”, and a @noun variable of “coffee”.
When we have a parser and a lexicon, we can get more advanced user input. The user’s input string no longer has to directly match our desired string input. After writing the parser, I wrote a test file to test all the functions. Writing the tests means I have to understand what the expected results for the functions are. I first make the test fail, and then make the test pass for each function in the parser.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s