Tsallaka zuwa abun ciki
Justice Tech School

if, elsif, else and unless

Conditions can end with a modifier. unless runs when false. The ternary is cond ? a : b.

Example
n = 15
if n % 15 == 0 then puts "FizzBuzz"
elsif n % 3 == 0 then puts "Fizz"
else puts n
end
puts "big" if n > 10
puts "no" unless n > 100
puts n.odd? ? "odd" : "even"

Result

FizzBuzz
big
no
odd

Exercise

Opposite of if.

 x > 5

Quiz Test

How do you write else if?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.