Ruby Conditional Statements

In Ruby, like in most programming languages, you can control the flow of your code using conditional statements. Here's an overview of the various types of conditional statements in Ruby:

1. If Statement:

The if statement is the most basic type of conditional. It runs a block of code if a certain condition is true:

x = 10

if x > 5
  puts "x is greater than 5"
end

2. Else Statement:

An else statement can be added to an if statement to provide a block of code that will run if the condition is not true:

x = 4

if x > 5
  puts "x is greater than 5"
else
  puts "x is not greater than 5"
end

3. Elsif Statement:

You can check multiple conditions using the elsif statement. Ruby will run the first block of code whose condition is true:

x = 10

if x > 20
  puts "x is greater than 20"
elsif x > 10
  puts "x is greater than 10"
else
  puts "x is not greater than 20 or 10"
end

4. Unless Statement:

The unless statement is the logical opposite of the if statement. It runs a block of code if a certain condition is not true:

x = 10

unless x > 20
  puts "x is not greater than 20"
end

You can also use else with unless:

x = 30

unless x > 20
  puts "x is not greater than 20"
else
  puts "x is greater than 20"
end

5. Ternary Operator:

The ternary operator (?:) is a shorthand way of writing an if-else statement. It takes a condition, a value to return if the condition is true, and a value to return if the condition is false:

x = 10

puts x > 5 ? "x is greater than 5" : "x is not greater than 5"

6. Case Statement:

The case statement allows you to compare a value against multiple patterns and run the first block of code that matches:

x = 10

case x
when 5
  puts "x is 5"
when 10
  puts "x is 10"
else
  puts "x is not 5 or 10"
end

These are the basic conditional statements in Ruby. By using these, you can control the flow of your code based on conditions.

  1. Using case statements in Ruby:

    • Description: Case statements in Ruby provide a way to perform multiple comparisons in a concise manner.
    • Code example:
      fruit = 'apple'
      
      case fruit
      when 'apple'
        puts 'It is an apple.'
      when 'banana'
        puts 'It is a banana.'
      else
        puts 'It is something else.'
      end
      
  2. Conditional operators in Ruby:

    • Description: Ruby has conditional operators like ==, !=, <, >, <=, and >= for comparisons.
    • Code example:
      x = 5
      y = 10
      
      puts "x is equal to y" if x == y
      
  3. Nested if statements in Ruby:

    • Description: You can nest if statements to handle multiple levels of conditions.
    • Code example:
      x = 5
      y = 10
      
      if x == y
        puts 'x is equal to y'
      else
        if x > y
          puts 'x is greater than y'
        else
          puts 'x is less than y'
        end
      end
      
  4. Ternary operator in Ruby:

    • Description: The ternary operator (condition ? true_value : false_value) provides a concise way for simple conditional expressions.
    • Code example:
      x = 5
      y = 10
      
      result = x > y ? 'x is greater' : 'x is not greater'
      puts result
      
  5. Comparing values with if statements in Ruby:

    • Description: Use if statements for conditional execution based on value comparisons.
    • Code example:
      temperature = 25
      
      if temperature > 30
        puts 'It is hot.'
      elsif temperature > 20
        puts 'It is warm.'
      else
        puts 'It is cool.'
      end
      
  6. Ruby conditional expressions:

    • Description: Conditional expressions in Ruby return values based on conditions.
    • Code example:
      x = 5
      y = 10
      
      result = if x > y
                 'x is greater'
               else
                 'x is not greater'
               end
      puts result
      
  7. Switch case in Ruby:

    • Description: The switch case statement (case-when) is an alternative to if-elsif-else for multiple comparisons.
    • Code example:
      fruit = 'apple'
      
      case fruit
      when 'apple'
        puts 'It is an apple.'
      when 'banana'
        puts 'It is a banana.'
      else
        puts 'It is something else.'
      end
      
  8. Handling multiple conditions in Ruby:

    • Description: Combine conditions using logical operators like && (and), || (or), and ! (not).
    • Code example:
      age = 25
      income = 50000
      
      if age > 21 && income > 30000
        puts 'You qualify for the loan.'
      else
        puts 'You do not qualify for the loan.'
      end
      
  9. Truthy and falsy values in Ruby:

    • Description: In Ruby, every value is truthy except nil and false, which are falsy.
    • Code example:
      value = 'Hello'
      
      if value
        puts 'The value is truthy.'
      else
        puts 'The value is falsy.'
      end
      
  10. Combining conditions with logical operators in Ruby:

    • Description: Use logical operators (&&, ||, !) to combine or negate conditions.
    • Code example:
      x = 5
      y = 10
      
      if x > 0 && y > 0
        puts 'Both x and y are positive.'
      end
      
      if x > 0 || y > 0
        puts 'At least one of x or y is positive.'
      end
      
      if !(x == y)
        puts 'x is not equal to y.'
      end