Skip to main content Link Search Menu Expand Document (external link)

Conditionals

This chapter will explore Ruby basic control structures.

Table of contents

  1. What’s conditionals
  2. True & False definition in Ruby
  3. Logic operation
  4. if & else statement
  5. unless statement
  6. case statement

What’s conditionals

I want to know how many years of Philippine Independence. create independent2years.rb

year_of_independent = 1898
current_year = ARGV[0].to_i
how_many_years = current_year - year_of_independent
puts how_many_years

Try this code

 $~ ruby independent2years.rb 2022
124

But it has some problems. Once I want to check 1811, the return value will be negative.

 $~ ruby independent2years.rb 1811
-87

We need to use conditionals, if current year earlier than year_of_independent, we need to show some messages.

True & False definition in Ruby

Trueexcept false and nil, other values are True
Falsefalse and nil

It has a convention, the method returns the value is true or false, the method naming ? should be the least letter.

irb(main):001:0> "".empty?
=> true
irb(main):002:0> "Koda".empty?
=> false

Logic operation

Ruby use &&(and) ||(or) in multiple conditions

condition_1 && condition_2

Both condition_1 and condition_2 are True. It will return true.

irb(main):001:0> true && true
=> true
irb(main):002:0> true && false
=> false
irb(main):003:0> false && true
=> false
irb(main):004:0> false || false
=> false
irb(main):005:0> false && 1
=> false
irb(main):006:0> true && 1
=> 1            -> if 1 is True
irb(main):007:0> 'a' && 1
=> 1            -> if 1 is True
irb(main):008:0> nil && 1
=> nil          -> if nil is False
irb(main):009:0> nil || nil
=> nil

condition_1 || condition_2

One of condition_1 or condition_2 is True, it will return true.

irb(main):001:0> true || true
=> true
irb(main):002:0> true || false
=> true
irb(main):003:0> false || true
=> true
irb(main):004:0> false || false
=> false
irb(main):005:0> false || 1
=> 1             -> if 1 is True
irb(main):006:0> true || 1
=> true
irb(main):0-7:0> 'a' || 1
=> "a"           -> if "a" is True
irb(main):008:0> nil || 1
=> 1             -> if 1 is True
irb(main):009:0> nil || nil
=> nil           -> if nil is False

if & else statement

if is the most basic conditional

Array example

if {condition}
  {code block}
end

irb(main):001:0> stop_light = 'red'
=> "red"
irb(main):002:0> if stop_light == 'red'
irb(main):003:1>   puts 'You should stop'
irb(main):004:1> end
You should stop
=> nil

Ruby if modifier

irb(main):001:0> stop_light = 'red'
=> "red"
irb(main):002:1> puts 'You should stop' if stop_light == 'red'
You should stop
=> nil

multiple conditionals

Array example

if {condition 1}
  {code block 1}
elsif {condition 2}
  {code block 2}
elsif {condition 3}
  {code block 3}
else
  {code block 4}
end

irb(main):001:0> stop_light = 'yellow'
=> "yellow"
irb(main):002:0> if stop_light == 'red'
irb(main):003:1>   puts 'You should stop'
irb(main):004:0> elsif stop_light == 'green'
irb(main):005:1>   puts 'You can go'
irb(main):006:0> elsif stop_light == 'yellow'
irb(main):007:1>   puts 'You should go carefully'
irb(main):008:0> else 
irb(main):009:1>   puts 'The Stop light not work'
irb(main):010:1> end
you should go carefully
=> nil

unless statement

unless is the exact opposite of if. It’s a negated if.

Array example

unless {condition}
  {code block}
end

irb(main):001:0> stop_light = 'green'
=> green
irb(main):002:0> unless stop_light == 'red'
irb(main):003:1>   puts 'You should go'
irb(main):004:1> end
You should go
=> nil

Array example

Ruby can put else with another code block

unless {condition}
  {code block 1}
else
  {code block 2}
end
irb(main):001:0> stop_light = 'red'
=> "red"
irb(main):002:0> unless stop_light == 'red'
irb(main):003:1>   puts 'The should go'
irb(main):004:0> else 
irb(main):005:1>   puts 'You should stop'
irb(main):006:1> end
You should stop
=> nil

Ruby unless modifier

irb(main):001:0> stop_light = 'green'
irb(main):002:1> puts 'You should go' unless stop_light == 'red'
You should go
=> nil

case statement

Whenever you need to use some if / elsif statements, you could consider using a Ruby case statement instead.

Array example

case {variable}
when {value 1}
  code block 1
when {value 2}
  code block 2
when {value 3}
  code block 3
else
  code block 4
end

irb(main):001:0> stop_light = 'yellow'
=> "yellow"
irb(main):002:0> case stop_light
irb(main):003:0> when 'red'
irb(main):004:1>   puts 'You should stop'
irb(main):005:0> when 'green'
irb(main):006:1>   puts 'You can go'
irb(main):007:0> when 'yellow'
irb(main):008:1>   puts 'You should go carefully'
irb(main):009:0> else 
irb(main):010:1>   puts 'The Stop light not work'
irb(main):011:1> end
you should go carefully
=> nil

another example, create file case_class.rb

# case_class.rb
array = ["a", 1, nil]
array.each do |variable|
  case variable
  when String
    puts "#{variable} is a String"
  when Numeric
    puts "#{variable} is a Numeric"
  else
    puts "#{variable} is something"
  end
end

run ruby case_class.rb

 $~ ruby case_class.rb
a is a String
1 is a Numeric
 is something

Back to top

Copyright © 2020-2022 Secure Smarter Service, Inc. This site is powered by KodaCamp.