Review
You've already seen literals: every time you type an object directly into Ruby code, you're using a literal. A literal, in terms of data declaration, is simply when the input value (the code you type) is exactly equal to the output value (how that value is printed). Take a look at the following example:
Rubymonk's STDOUT
is slightly different from that of irb
(if you're following along at home). If you have your own Ruby process running, you'll notice that calling .inspect
on the array prints the array literal within a string literal. In rubymonk, the string is stripped but the effect is the same -- you can see that when Ruby outputs an array, it looks like like the array we typed ourselves.
Try it a few times yourself. Each of the methods below should return the literal it describes:
Click on "See the Solution" above -- notice how array and hash literals don't care about whitespace. This can come in handy when we want to format our Ruby data like a document -- notice that the an_array_of_hashes
and an_array_of_arrays
method bodies already look somewhat tabular.