Conditions ruby

Estimated reading time: 1 minute

flow control

  • operators

<, >, <=, >=, ==, !=, &&, ||

if

if 5 == 5
  puts('number is 5')
else
  puts('number is not 5')
end
# number is 5

elsif

  • else if short elsif
puts "Put in a number"
a = gets.chomp.to_i

if a == 3
  puts "a is 3"
elsif a == 4
  puts "a is 4"
else
  puts "a is neither 3, nor 4"
end

case

case is basically switch case

a = 5

case a
when 5
  puts "a is 5"
when 6
  puts "a is 6"
else
  puts "a is neither 5, nor 6"
end

source

ruby