In Ruby, control structures are used to control the flow of execution in a program. There are several types of control structures in Ruby, including:
.jpg)
1. Conditional Statements
Conditional statements are used to execute different blocks of code based on certain conditions. The most common conditional statements in Ruby are:
- if/else statements
- case statements
- ternary operator
if/else statements allow you to execute different blocks of code depending on whether a condition is true or false. For example:
x = 10
if x > 5
puts "x is greater than 5"
else
puts "x is less than or equal to 5"
end
Case Statements :
Case statements allow you to evaluate a variable against multiple values and execute different blocks of code based on the match. For example:
day = "Monday"
case day
when "Monday"
puts "It's Monday"
when "Tuesday"
puts "It's Tuesday"
when "Wednesday"
puts "It's Wednesday"
else
puts "It's another day"
end
Ternary Operator :
x = 10
x > 5 ? puts("x is greater than 5") : puts("x is less than or equal to 5")
2. Loops
Loops are used to repeat a block of code until a certain condition is met. The most common loops in Ruby are:
- while loops
- until loops
- for loops
- each loops
While loops execute a block of code while a certain condition is true. For example:
i = 0
while i < 5
puts i
i += 1
end
Until Loops :
Until loops execute a block of code until a certain condition is true. For example:
i = 0
until i >= 5
puts i
i += 1
end
For Loops :
For loops iterate over a range of values and execute a block of code for each value. For example:
for i in 0..4
puts i
end
Each Loops :
Each loops are used to iterate over an array or hash and execute a block of code for each element. For example:
arr = [1, 2, 3, 4, 5]
arr.each do |i|
puts i
end
3. Control Flow Statements
Control flow statements allow you to change the flow of execution in a program. The most common control flow statements in Ruby are:- break
- next
- redo
The break statement is used to exit a loop early. For example:
i = 0
while i < 5
puts i
break if i == 2
i += 1
end
Next
The next statement is used to skip to the next iteration of a loop. For example:
i = 0
while i < 5
i += 1
next if i == 2
puts i
end
Redo
The redo statement is used to repeat the current iteration of a loop. For example:
i = 0
while i < 5
i += 1
redo if i == 2
puts i
end
4. Exception Handling
Exception handling is used to handle errors and unexpected conditions in a program. In Ruby, you can use the begin/rescue/end keywords to catch and handle exceptions. For example:
begin # some code that might raise an exception rescue # code to handle the exception end
You can also specify which type of exception to catch by using the rescue keyword followed by the exception type. For example
begin
# some code that might raise a ZeroDivisionError
1/0
rescue ZeroDivisionError
puts "Can't divide by zero!"
end
5. Blocks
Blocks are used to encapsulate a piece of code that can be passed around and executed at a later time. Blocks are defined using either the do/end keywords or curly braces {}. For example:
# using do/end
[1, 2, 3].each do |i|
puts i
end
# using curly braces
[1, 2, 3].each { |i| puts i }
Blocks can also be passed as arguments to methods. For example:
def my_method
yield
end
my_method do
puts "Hello, world!"
end
This will output "Hello, world!" when my_method is called.
Overall, control structures are a critical part of Ruby programming, allowing you to control the flow of execution in your program, handle errors, and encapsulate code for reuse.

0 Comments