--- # Tests from the (Poignant Guide)
in: >
h3. False
!
if plastic_cup
print "Plastic cup is on the up 'n' up!"
end
If @plastic_cup@ contains either @nil@ or @false@, you won't see anything print
to the screen. They're not on the @if@ guest list. So @if@ isn't going to run
any of the code it's protecting.
But @nil@ and @false@ need not walk away in shame. They may be of questionable
character, but @unless@ runs a smaller establishment that caters to the bedraggled.
The @unless@ keyword has a policy of only allowing those with a negative charge in.
Who are: @nil@ and @false@.
unless plastic_cup
print "Plastic cup is on the down low."
end
You can also use @if@ and @unless@ at the end of a single line of code, if that's
all that is being protected.
print "Yeah, plastic cup is up again!" if plastic_cup
print "Hardly. It's down." unless plastic_cup
Now that you've met @false@, I'm sure you can see what's on next.
out: "False
\n\n\t
The cat Trady Blix. Frozen in emptiness. Immaculate whiskers rigid. Placid eyes of lake. Tail of warm icicle. Sponsored by a Very Powerful Pause Button.
\n\n\tThe darkness surrounding Blix can be called negative space. Hang on to that phrase. Let it suggest that the emptiness has a negative connotation. In a similar way, nil
has a slightly sour note that it whistles.
Generally speaking, everything in Ruby has a positive charge to it. This spark flows through strings, numbers, regexps, all of it. Only two keywords wear a shady cloak: nil
and false
draggin us down.
You can test that charge with an if
keyword. It looks very much like the do
blocks we saw in the last chapter, in that both end with an end
.
\n if plastic_cup\n print \"Plastic cup is on the up 'n' up!\" \n end\n\n\n\t
If plastic_cup
contains either nil
or false
, you won’t see anything print to the screen. They’re not on the if
guest list. So if
isn’t going to run any of the code it’s protecting.
But nil
and false
need not walk away in shame. They may be of questionable character, but unless
runs a smaller establishment that caters to the bedraggled. The unless
keyword has a policy of only allowing those with a negative charge in. Who are: nil
and false
.
\n unless plastic_cup\n print \"Plastic cup is on the down low.\" \n end\n\n\n\t
You can also use if
and unless
at the end of a single line of code, if that’s all that is being protected.
\n print \"Yeah, plastic cup is up again!\" if plastic_cup\n print \"Hardly. It's down.\" unless plastic_cup\n\n\n\t
Now that you’ve met false
, I’m sure you can see what’s on next.
\n