Ruby Tutorial
Ruby CGI
Ruby Advanced
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.
Using case statements in Ruby:
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
Conditional operators in Ruby:
==
, !=
, <
, >
, <=
, and >=
for comparisons.x = 5 y = 10 puts "x is equal to y" if x == y
Nested if statements in Ruby:
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
Ternary operator in Ruby:
condition ? true_value : false_value
) provides a concise way for simple conditional expressions.x = 5 y = 10 result = x > y ? 'x is greater' : 'x is not greater' puts result
Comparing values with if statements in Ruby:
temperature = 25 if temperature > 30 puts 'It is hot.' elsif temperature > 20 puts 'It is warm.' else puts 'It is cool.' end
Ruby conditional expressions:
x = 5 y = 10 result = if x > y 'x is greater' else 'x is not greater' end puts result
Switch case in Ruby:
case-when
) is an alternative to if-elsif-else for multiple comparisons.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
Handling multiple conditions in Ruby:
&&
(and), ||
(or), and !
(not).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
Truthy and falsy values in Ruby:
nil
and false
, which are falsy.value = 'Hello' if value puts 'The value is truthy.' else puts 'The value is falsy.' end
Combining conditions with logical operators in Ruby:
&&
, ||
, !
) to combine or negate conditions.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