6.1 Literals

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:

Example Code:

Output Window

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:

Output Window

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.

A few new tricks

Thankfully, the Ruby's literal syntax is usually exactly what we expect: Type a number, get a number. Type a string, get a string. Literals are not without nuance, however. Here are some forms you might not guess. Let's start with numbers. Everything starts with numbers.

Example Code:

Output Window

The e-30 in the above example is scientific notation for "less three decimal places". The underscore, however, means absolutely nothing; you'll notice it wasn't printed when that code was executed. It's simply a convenience for writing out large numbers which can become difficult to read without demarcation. Try using underscores to make a huge, readable number. Assume you have access to the describe method from here on out. We've hidden it to avoid cluttering up the next few examples and exercises.

Output Window

Pretty easy! And that's really the whole point... it should be easy to create a new object of the types we use everywhere, such as numbers, arrays, and strings. Speaking of strings! Let's take a look at a couple of forms you may have not encountered in your Ruby programming yet.

Example Code:

Output Window

Try to guess the string literal forms required for the following three exercises. The first two will use a technique you just saw (escaping). The third exercise might require a hint because it's a little weird. See if you can get it without the hint, though! If you've been reading other Ruby code, you may have seen it elsewhere.

Output Window

Output Window

Hint

Ruby has a `%Q` syntax which allows you to use any character as the string-quote character. For example, %Q[quotes are "neat".] is valid Ruby. Within the `%Q` syntax, you do not need to escape quotes, double-quotes, or newlines!

Output Window

Collections

The Array literal makes a lot of sense once you've seen it: [1, 2, 3]. It's quite common to create arrays of strings, however, and typing all those quotes can be a nuisance. They also make things harder to read if they just appear repetitive. The array-of-strings literal is similar to the big-q literal you just saw. In fact, it's so similar... heck, why don't you try to guess it?

Hint

Ruby has a `%w` and `%W` (interpolated) syntax, which works just like `%Q` except it produces an array of words instead of a string.

Output Window

Okay, last one! This literal actually has a little more meat to it since it's a data type we haven't covered in depth yet: ranges. Ruby has the concept of a range of values; given a start and an end point, Ruby will fill in the blanks. It looks like this:

Example Code:

Output Window

Here, we see a few interesting properties of ranges. First off, ranger_rick is defined using variables embedded in the Range literal. This makes sense when we think about those two little dots as being no different from the square brackets we use in arrays... but it's not immediately obvious that this is allowed. Next, we see that a range is a collection. We can map over the range to transform it into an array containing all the points explicitly. Range includes Enumerable, which gives us access to all its elements.

Now try a couple for yourself. Discover the syntax for the "one less" literal (which excludes the latter element) and for the range-of-characters literal. Note: I won't be using Range to test if you got the right answer, because that would print the literal for you. The tests will compare ranges mapped into arrays.

Hint

Try adding one more dot?

Output Window

Hint

Try using variables like we did at the start of this section if you're confused about the specific syntax.

Output Window

There are still more literals in Ruby for you to explore, though these are the most common. We'll take a look at these more advanced literals later on when we cover the topics they govern. Before you leave this chapter, experiment on your own to see if you can create negative ranges!

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2024 by Jasim A Basheer