Boolean Logic

You use Boolean logic to make yes/no decisions. As it happens, there is a considerable similarity between Boolean logic and the binary arithmetic we've just been looking at; both involve and and or operations as well as negations. The main difference is that we no longer care about the individual bits. As far as Boolean logic is concerned, an object can be considered only true or false, and so it might as well have only one bit.

Recall from Day 4 that Ruby lives by the simple (though perhaps unconventional) rule that the only values that count as false are false and nil, and everything else is true, as the following code demonstrates:

 def show_if_true(ob) p ob if ob end show_if_true false show_if_true true show_if_true nil show_if_true ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.