Conditional Logic (if)
Ruby's
if statement is clean and straightforward.ruby
The unless Statement
Wait! Ruby has a special keyword for the negative case:
unless. It means "if not."ruby
Statement Modifiers
You can write
if and unless at the end of a line for very concise, English-like code.ruby
The case Statement
The
case statement is Ruby's version of a switch. It is very flexible and uses the "Triple Equals" (===) operator under the hood.ruby
Iteration vs. Looping
While Ruby has
while and until loops, Rubyists almost never use them. Instead, we use Iterators (Methods that take blocks).ruby
Summary
| Keyword | Description |
|---|---|
| if / elsif | Standard branching |
| unless | Branching if a condition is FALSE |
| case | Multi-way branch |
| times | Repeating code N times |
| each | Iterating through a collection |
| Modifier | code if condition (Post-fix syntax) |
| Logic | Favor iterators over manual while loops |
| Safety | Ruby uses "Truthy" (Everything is true except false and nil) |
| Modern | Case statements can match against classes and regex |
| Standard | Indent with 2 spaces in Ruby |
| Identity | Control flow returns the value of the last executed line |