Exercise 25: Even More Practice
We're going to do some more practice involving functions and variables to make sure you know them well. This exercise should be straightforward for you to type in, break down, and understand.
However, this exercise is a little different. You won't be running it. Instead you will import it into Ruby and run the functions yourself.
First, run this with ruby ex25.rb to find any errors you have made. Once you have found all of the errors you can and fixed them, you will then want to follow the What You Should See section to complete the exercise.
What You Should See
In this exercise we're going to interact with your ex25.rb file inside the irb interpreter you used periodically to do calculations. You run irb from the terminal like this:
>>> $ irb irb(main):001:0>
Your output should look like mine, and after the > character (called the prompt) you can type Ruby code in, and it will run immediately. Using this I want you to type each of these lines of Ruby code into irb and see what it does:
Here's what it looks like when I work with the ex25.rb module in irb:
>> require "./ex25.rb"
=> true
>>
?> sentence = "All good things come to those who wait."
=> "All good things come to those who wait."
>> words = Ex25.break_words(sentence)
=> ["All", "good", "things", "come", "to", "those", "who", "wait."]
>> words
=> ["All", "good", "things", "come", "to", "those", "who", "wait."]
>> sorted_words = Ex25.sort_words(words)
=> ["All", "come", "good", "things", "those", "to", "wait.", "who"]
>> sorted_words
=> ["All", "come", "good", "things", "those", "to", "wait.", "who"]
>> Ex25.print_first_word(words)
All
=> nil
>> Ex25.print_last_word(words)
wait.
=> nil
>> words
=> ["good", "things", "come", "to", "those", "who"]
>> Ex25.print_first_word(sorted_words)
All
=> nil
>> Ex25.print_last_word(sorted_words)
who
=> nil
>> sorted_words
=> ["come", "good", "things", "those", "to", "wait."]
>> sorted_words = Ex25.sort_sentence(sentence)
=> ["All", "come", "good", "things", "those", "to", "wait.", "who"]
>> sorted_words
=> ["All", "come", "good", "things", "those", "to", "wait.", "who"]
>> Ex25.print_first_and_last(sentence)
All
wait.
=> nil
>> Ex25.print_first_and_last_sorted(sentence)
All
who
=> nil
As you go through each of these lines, make sure you can find the function that's being run in ex25.rb and you understand how each one works. If you get different results or error, you'll have to go fix your code, exit irb, and start over.
Study Drills
- Take the remaining lines of the What You Should See output and figure out what they are doing. Make sure you understand how you are running your functions in the ex25 module.
- The Ex25 module doesn't have to be in a file named ex25.rb. Try putting it in a new file with a random name, then import that file and see how you still have Ex25 available even though the file you made does not have ex25 in it.
- Try breaking your file and see what it looks like in irb when you use it. You will have to quit irb with quit() to be able to reload it.
Common Student Questions
- How can the words.pop function change the words variable?
- That's a complicated question, but in this case words is a list, and because of that you can give it commands, and it'll retain the results of those commands. This is similar to how files and many other things worked when you were working with them.
- When should I puts instead of return in a function?
- The return from a function gives the line of code that called the function a result. You can think of a function as taking input through its arguments and returning output through return. The puts is completely unrelated to this and only deals with printing output to the terminal.