Other Pages

Expand All



Computers have a very strict idea of when things are true and false.

(Unlike Stephen Colbert...)

True or False?

Try the following in irb:


The magic word if is called a CONDITIONAL.

if age < 18 then
  puts "Sorry, adults only."

One-Line Condition

Ruby has a compact way of putting an entire if expression on one line:

puts "Sorry, adults only." if age < 18

Note that:

if... then... else... end

The magic word else allows BRANCHING.

if age >= 18 then
  puts "allowed"
  puts "denied"

Like a fork in the road, the program chooses one path or the other.

(In Ruby, then is optional, so we usually leave it off, but if it makes your code clearer, go ahead and use it.)

2 + 2 = 4

Sadly, this expression:

2 + 2 = 4

causes a SyntaxError. You need to do

2 + 2 == 4

instead. Why?

The Tragedy of the Equal Sign

This is confusing, and you should feel confused.

LAB: Good Friend, Bad Friend

Conjunction Junction

LAB: Enemies List

Back to Learn To Code

If you find something that could be improved, please make a pull request or drop us a note via GitHub Issues (no technical knowledge required).

Source: https://github.com/RailsBridge-CapeTown