Other Pages

Slides

Logic

Truthiness

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

Truthiness

(Unlike Stephen Colbert...)

True or False?

Try the following in irb:

Conditions

The magic word if is called a CONDITIONAL.

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

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"
else
  puts "denied"
end

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