Home > Articles > Programming > General Programming/Other Languages

Containers in Ruby

  • Print
  • + Share This
Similar to a grocery bag, containers hold a variety of objects and make life easier. Learn how to bag your objects into strings, arrays, hashes, and ranges.
This chapter is from the book

This chapter is from the book

"It's a Useful Pot," said Pooh. "Here it is. And it's got 'A Very Happy Birthday with love from Pooh' written on it. That's what all that writing is. And it's for putting things in. There!"

—A. A. Milne, Winnie-the-Pooh

Today we look at objects whose only purpose in life is to hold other objects. Containers make programming more convenient in the same way that grocery bags make it easier to get your food home from the store: They reduce the number of objects you have to handle and the number of names you have to remember. You don't have to think about which food items are in which bag when you are picking up the bags and carrying them into the house.

Before we pick up that agenda though, there are two digressions to make.

A Change in Convention

Up to this point, most examples you've seen have been either scripts or parts of irb sessions. irb is a nice learning tool, because it lets you see the effects of a line of code immediately. Scripts are the form of the actual work you will do in Ruby, and they let you control exactly what information appears in the output.

From this point forward, to save space and clutter most of our code examples will appear naked, meaning that you can either type them into irb or run them in script form. Let's illustrate with a short piece of code, then walk through an equivalent irb session.

 ltrs = ("r" .. "u").to_a #–> ["r", "s", "t", "u"]
 ltrs[1].capitalize!  # ltrs == ["r", "S", "t", "u"]
 puts ltrs.length   # 4

Comments have been added to show some relevant expression values and outputs. When showing the value of the given expression (as in the first line), the comment starts with ->. Some comments will also show value of some other named expression (as in the second line), or the output that appears as a result of evaluating the given expression (as in the third line).

If you ran this example as a script, possibly after inserting the appropriate "shebang" line at the top, you would see only the number 4. No other output was explicitly requested.

An equivalent irb session might look like this.

 irb(main):001:0> ltrs = ("r" .. "u").to_a
 ["r", "s", "t", "u"]
 irb(main):002:0> ltrs[1].capitalize!
 "S"
 irb(main):003:0> ltrs
 ["r", "S", "t", "u"]
 irb(main):004:0> puts ltrs.length
 4
 nil

The expression in line 2 failed to show us the whole list of letters, so in line 3 we asked specifically to see the list. Line 4 gave us the output that we were interested in, along with the nil evaluation of the puts statement, which we probably didn't care about. When writing code samples from now on, we'll just document the values and outputs that are interesting to us. You can use irb or write small scripts to verify the results and try your own experiments.

  • + Share This
  • 🔖 Save To Your Account