Tuesday, November 22, 2011

Exercise 25: Even More Practice: Learn Ruby the Hard Way: Practicum

Here's a bit of a change-up. We are used to running functions in files and giving them values from a command line or from STDIN (i.e. the keyboard) but we can also interactively pull them into IRB and, well, interact with them. This lesson also introduces us to the idea of a module and the actual code term "module". Have you added that to your list of terms yet :)?

Let's see what happens when we import a module into the Ruby interpreter and run the functions ourselves.

And here's what it looks like in IRB

Let's break this down line by line to make sure you know what's going on:

Line 2 you require your ./ex25.rb Ruby file, just like other requires you have done. Notice you do not need to put the .rb at the end to require it. When you do this you make a module that has all your functions in it to use.

Line 4 you made a sentence to work with.

Line 6 you use the Ex25 module and call your first function Ex25.break_words. The . (dot, period) symbol is how you tell Ruby, "Hey, inside Ex25 there's a function called break_words and I want to run it."

Line 8 we do the same thing with Ex25.sort_words to get a sorted sentence.

Lines 10-15 we use Ex25.print_first_word and Ex25.print_last_word to get the first and last word printed out.

Line 16 is interesting. I made a mistake and typed the words variable as "wrods" so Ruby gave me an error on Lines 17-18.

Lines 19-20 is where we print the modified words list. Notice that since we printed the first and last one, those words are now missing.

The remaining lines are for you to figure out and analyze in the extra credit.

Extra Credit

Take the remaining lines of the WYSS output and figure out what they are doing. Make sure you understand how you are running your functions in the Ex25 module.

[In my file, Ex25.sort_sentence(sentence) prints out the full sentence and lists the words in alphabetical order. Ex.25.print_first_and_last(sentence) does what it says it prints the sorted first word and the last word from the original, unmodified sentence. Ex25.print_first_and_last_sorted(sentence) takes the sorted words and prints out the first and lat words that would be in alphabetical order.]

The reason we put our functions in a module is so they have their own namespace. If someone else writes a function called break_words, we won't collide. However, if typing Ex25. is annoying, you can type include Ex25 which is like saying, "Include everything from the Ex25 module in my current module."

[Your mileage may vary on this one, but when I type "include Ex25" I get an output from the interpreter saying object, but then if I try to type function calls that don't include the Ex25, I get errors:

Actually, I don't mind this, I like the idea of calling a module and having the module be defined and have the leading name. It makes it easier for me to see the connection.]

Try breaking your file and see what it looks like in Ruby when you use it. You will have to quit IRB with CTRL-D to be able to reload it.

[Typos are fun, and changing out statements can lead to interesting errors. I'd still like to know why "include Ex25" isn't behaving like it says it should, but again, for me personally, I don't see it as a huge deal breaker.]


This is pretty cool. I like the fact that we can define modules in separate files, call them, and have those modules keep separate name space. While it might get confusing, I could imagine a need for very similar functions but those that work slightly different. rather than having one file with multiple descriptions or branching paths, having two separate modules to call and using them with the name designator is a good way to tell which version you are using.

No comments: