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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Ex25

  # This function will break up words for us.
  def Ex25.break_words(stuff)
    words = stuff.split(' ')
    return words
  end

  # Sorts the words.
  def Ex25.sort_words(words)
    return words.sort
  end

  # Prints the first word after shifting it off.
  def Ex25.print_first_word(words)
    word = words.shift
    puts word
  end

  # Prints the last word after popping it off.
  def Ex25.print_last_word(words)
    word = words.pop
    puts word
  end

  # Takes in a full sentence and returns the sorted words.
  def Ex25.sort_sentence(sentence)
    words = Ex25.break_words(sentence)
    return Ex25.sort_words(words)
  end

  # Prints the first and last words of the sentence.
  def Ex25.print_first_and_last(sentence)
    words = Ex25.break_words(sentence)
    Ex25.print_first_word(words)
    Ex25.print_last_word(words)
  end

  # Sorts the words then prints the first and last one.
  def Ex25.print_first_and_last_sorted(sentence)
    words = Ex25.sort_sentence(sentence)
    Ex25.print_first_word(words)
    Ex25.print_last_word(words)
  end

end

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
require "./ex25.rb"

sentence = "All good things come to those who wait."
words = Ex25.break_words(sentence)
words
sorted_words = Ex25.sort_words(words)
sorted_words
Ex25.print_first_word(words)
Ex25.print_last_word(words)
words
Ex25.print_first_word(sorted_words)
Ex25.print_last_word(sorted_words)
sorted_words
sorted_words = Ex25.sort_sentence(sentence)
sorted_words
Ex25.print_first_and_last(sentence)
Ex25.print_first_and_last_sorted(sentence)

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

  1. 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.
  2. 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.
  3. 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.

Buy DRM-Free

When you buy directly from the author, Zed A. Shaw, you'll get a professional quality PDF and hours of HD Video, all DRM-free and yours to download.

$29.99

Buy Directly From The Author

Or, you can read Learn Ruby the Hard Way for free right here, video lectures not included.