Conditional Execution

Ruby has several different mechanisms for the conditional execution of code; they should feel similar to other programming languages, but many have some neat twists. Before we get into them, we need to spend a short time looking at boolean expressions.

Boolean Expressions

Ruby has a simple definition of truth. Any value that isn’t nil or the constant false is true"cat", 99, 0, and :a_song—are all considered true. An empty string "", an empty array [], and an empty hash {} are all true in Ruby. (You’ll sometimes see Rubyists refer to the set of all false values as “falsey” and the set of all true values as “truthy”.)

In this book, when we want to talk about a general true or false value, we use regular Roman type: true ...

Get Programming Ruby 3.3 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.