Control structures are fundamental in programming as they allow you to control the flow of your program. In Ruby, control structures include conditionals and loops, which help you make decisions and repeat actions. This section will cover the following topics:
- If-Else Statements
- Unless Statements
- Case Statements
- Ternary Operator
- If-Else Statements
The if-else statement is used to execute code based on a condition. If the condition evaluates to true, the code inside the if block is executed. Otherwise, the code inside the else block is executed.
Syntax
if condition # code to execute if condition is true else # code to execute if condition is false end
Example
age = 18 if age >= 18 puts "You are eligible to vote." else puts "You are not eligible to vote." end
Explanation
age >= 18is the condition.- If
ageis greater than or equal to 18, the message "You are eligible to vote." is printed. - Otherwise, the message "You are not eligible to vote." is printed.
- Unless Statements
The unless statement is the opposite of the if statement. It executes the code block only if the condition is false.
Syntax
unless condition # code to execute if condition is false else # code to execute if condition is true end
Example
age = 16 unless age >= 18 puts "You are not eligible to vote." else puts "You are eligible to vote." end
Explanation
age >= 18is the condition.- If
ageis not greater than or equal to 18, the message "You are not eligible to vote." is printed. - Otherwise, the message "You are eligible to vote." is printed.
- Case Statements
The case statement is used to execute one block of code out of many based on the value of an expression. It is similar to the switch statement in other programming languages.
Syntax
case expression when value1 # code to execute if expression == value1 when value2 # code to execute if expression == value2 else # code to execute if expression does not match any value end
Example
day = "Saturday" case day when "Monday" puts "Start of the work week." when "Friday" puts "End of the work week." when "Saturday", "Sunday" puts "Weekend!" else puts "Midweek day." end
Explanation
dayis the expression.- If
dayis "Monday", the message "Start of the work week." is printed. - If
dayis "Friday", the message "End of the work week." is printed. - If
dayis "Saturday" or "Sunday", the message "Weekend!" is printed. - If
daydoes not match any of the specified values, the message "Midweek day." is printed.
- Ternary Operator
The ternary operator is a shorthand for the if-else statement. It is used to execute one of two expressions based on a condition.
Syntax
Example
age = 20 message = age >= 18 ? "You are eligible to vote." : "You are not eligible to vote." puts message
Explanation
age >= 18is the condition.- If
ageis greater than or equal to 18, the expression"You are eligible to vote."is assigned tomessage. - Otherwise, the expression
"You are not eligible to vote."is assigned tomessage. - The value of
messageis then printed.
Practical Exercises
Exercise 1: If-Else Statement
Write a Ruby program that checks if a number is positive, negative, or zero.
Solution
number = -5 if number > 0 puts "The number is positive." elsif number < 0 puts "The number is negative." else puts "The number is zero." end
Exercise 2: Case Statement
Write a Ruby program that takes a grade (A, B, C, D, F) and prints the corresponding message.
Solution
grade = "B" case grade when "A" puts "Excellent!" when "B" puts "Good job!" when "C" puts "Well done!" when "D" puts "You passed." when "F" puts "Better luck next time." else puts "Invalid grade." end
Exercise 3: Ternary Operator
Write a Ruby program that checks if a person is an adult (age 18 or older) using the ternary operator.
Solution
Common Mistakes and Tips
- Common Mistake: Forgetting to use
elsifinstead ofelse ifin Ruby.- Tip: Remember that Ruby uses
elsiffor additional conditions in anifstatement.
- Tip: Remember that Ruby uses
- Common Mistake: Using
=instead of==for comparison.- Tip: Use
==to compare values and=to assign values.
- Tip: Use
Conclusion
In this section, you learned about control structures in Ruby, including if-else, unless, case statements, and the ternary operator. These constructs allow you to control the flow of your program based on conditions. Practice the exercises to reinforce your understanding, and you'll be ready to move on to loops in the next section.
Ruby Programming Course
Module 1: Introduction to Ruby
Module 2: Basic Ruby Concepts
Module 3: Working with Collections
Module 4: Object-Oriented Programming in Ruby
- Classes and Objects
- Instance Variables and Methods
- Class Variables and Methods
- Inheritance
- Modules and Mixins
Module 5: Advanced Ruby Concepts
Module 6: Ruby on Rails Introduction
- What is Ruby on Rails?
- Setting Up Rails Environment
- Creating a Simple Rails Application
- MVC Architecture
- Routing
Module 7: Testing in Ruby
- Introduction to Testing
- Unit Testing with Minitest
- Behavior-Driven Development with RSpec
- Mocking and Stubbing
