3 m read

Control Structures in Ruby

Introduction to Control Structures in Ruby

In Ruby, control structures conditional statements play a vital role in altering the logic and flow of code execution.

They decide which block of code will execute based on certain predefined conditions.

The ‘if’ Conditional Statement

The ‘if’ statement is a commonly used control structure in Ruby. It evaluates a condition, and if it is true, it executes a certain block of code. If the condition is false, it moves on to the else block if available.


if condition
  # executes this block if condition is true
else
  # executes this block if condition is false
end
Code language: PHP (php)

The ‘unless’ Conditional Statement

The ‘unless’ statement is the inverse of ‘if’. The code block within ‘unless’ will execute if the test condition is false.


unless condition
  # executes this block if condition is false
else
  # executes this block if condition is true
end
Code language: PHP (php)

The ‘elsif’ Statement

To add more conditions and control in your Ruby scripts, you can use the ‘elsif’ feature. Note it’s ‘elsif’ and not ‘elseif’.


if condition1
  # executes this block if condition1 is true
elsif condition2
  # executes this block if condition1 is false and condition2 is true
else
  # executes this block if both conditions are false
end
Code language: PHP (php)

Ternary Operator

Ruby offers a short-if expression known as the ternary operator: a concise way to evaluate one statement over another.


condition ? # runs this if condition is true : # runs this if condition is false
Code language: PHP (php)

Loop Control Statements in Ruby

Loop control statements in Ruby are used to manage the execution of a loop, allowing it to either continue its execution or terminate it.

The ‘while’ Statement

A ‘while’ loop in Ruby continues to execute a block of code till a given condition is true. This statement is analogous to the ‘if’ statement but designed for continual execution until a condition is no longer met.


while condition
  # executes this block as long as the condition is true
end
Code language: PHP (php)

The ‘until’ Statement

In contrast to ‘while’, the ‘until’ statement executes a code block if a certain condition is false. Once the condition rings true, the loop terminates.


until condition
  # executes this block as long as the condition is false
end
Code language: PHP (php)

The ‘break’ Statement

The ‘break’ statement terminates an ongoing loop and prevents further iterations. This control statement is handy for stopping loop execution based on some internal condition.


while condition
  # some code here
  break if another_condition
  # more code here
end
Code language: PHP (php)

The ‘next’ Statement

The ‘next’ statement is used to terminate the current iteration of a loop and immediately jump to the next iteration.


while condition
  # some code here
  next if another_condition
  # more code here
end
Code language: PHP (php)

Return and Block Result Values in Ruby

Ruby uses return statements and block result values to control data flow within and between methods and blocks.

The ‘return’ Statement

The ‘return’ statement is used to pass a value back out of a method, terminating its execution and passing control back to the caller.


def some_method
  # some code here
  return value if condition
  # more code here
end
Code language: PHP (php)

Block Result Values

In Ruby, when an action is given in conditions, it may include a value to be used as a block result. If no ‘return’ statement is found, the block will still return a value, essentially the value of its last line.


block do |variable|
  # some code here
  value if condition # this will be the return value of the block
end
Code language: PHP (php)

Conclusion

In summary, understanding control structures in Ruby is a critical aspect of mastering the language. These structures provide the necessary control to manage the flow of program execution by using various conditionals and loop structures. From ‘if’ and ‘unless’ conditions to ‘while’ or ‘until’ loops, the ability to manage how the program executes offers developers a high level of control and extensibility in designing their code.

If you missed our previous tutorial, you could catch up here: “Getting Started with Operators in Ruby“.

Happy Coding!

Benji

Leave a Reply