Conditional Logic
Truthy and falsy in Ruby
The only false values in Ruby are the values nil
and false
themselves, the number 0 or an empty string (“”) aren't false in Ruby.
Basic Conditional Statement
if
statement:
if statement_to_be_evaluated == true
# do something awesome...
end
if 1 < 2
puts "Hot diggity, 1 is less than 2!"
end
#=> Hot diggity, 1 is less than 2!
If there is only one line of code to be evaluated inside the block, then you can rewrite the code to be more succinct and take up only one line:
puts "Hot diggity damn, 1 is less than 2" if 1 < 2
Adding else and elsif
if...elsif...else
statement:
if attack_by_land == true
puts "release the goat"
elsif attack_by_sea == true
puts "release the shark"
else
puts "release Kevin the octopus"
end
Boolean Logic
==
(equals) returns true
if the values compared are equal.
5 == 5 #=> true
5 == 6 #=> false
!=
(not equal) returns true
if the values compared are not equal.
5 != 7 #=> true
5 != 5 #=> false
>
(greater than) returns true
if the value on the left of the operator is larger than the value on the right.
7 > 5 #=> true
5 > 7 #=> false
<
(less than) returns true
if the value on the left of the operator is smaller than the value on the right.
5 < 7 #=> true
7 < 5 #=> false
>=
(greater than or equal to) returns true
if the value on the left of the operator is larger than or equal to the value on the right.
7 >= 7 #=> true
7 >= 5 #=> true
<=
(less than or equal to) returns true
if the value on the left of the operator is smaller than or equal to the value on the right.
5 <= 5 #=> true
5 <= 7 #=> true
#eql?
checks both the value type and the actual value it holds.
5.eql?(5.0) #=> false; although they are the same value, one is an integer and the other is a float
5.eql?(5) #=> true
#equal?
checks whether both values are the exact same object in memory. This can be slightly confusing because of the way computers store some values for efficiency. Two variables pointing to the same number will usually return true
.
a = 5
b = 5
a.equal?(b) #=> true
Ruby has a special operator that is affectionately referred to as the spaceship operator. Unlike the other comparison operators, which all return true
or false
, the spaceship operator returns one of three numerical values.
<=>
(spaceship operator) returns the following:
-
-1
if the value on the left is less than the value on the right; -
0
if the value on the left is equal to the value on the right; and -
1
if the value on the left is greater than the value on the right.
5 <=> 10 #=> -1
10 <=> 10 #=> 0
10 <=> 5 #=> 1
Logical Operators
&&
operator returns true
if both the left and right expressions return true
.
|| operator returns true
if one of the expressions return true
.
In Ruby there is a short circuit evaluation, if the first expression in &&
operator returns false
, the second expression is never checked. And if the first expression in || operator returns true
, the second expression is never checked, because it only needs one expression to be true.
The !
operator reverses the logic of the expression.
Case Statements
Use when you have to write multiple conditions:
grade = 'F'
did_i_pass = case grade #=> create a variable `did_i_pass` and assign the result of a call to case with the variable grade passed in
when 'A' then "Hell yeah!"
when 'D' then "Don't tell your mother."
else "'YOU SHALL NOT PASS!' -Gandalf"
end
If you need to do more code manipulation:
grade = 'F'
case grade
when 'A'
puts "You're a genius"
future_bank_account_balance = 5_000_000
when 'D'
puts "Better luck next time"
can_i_retire_soon = false
else
puts "'YOU SHALL NOT PASS!' -Gandalf"
fml = true
end
Unless Statements
An unless
statement works in the opposite way as an if
statement: if the condition is false, it processes the code.
age = 19
puts "Welcome to a life of debt." unless age < 18
unless age < 18
puts "Down with that sort of thing."
else
puts "Careful now!"
end
Ternary Operator
The ternary operator is a one-line if...else
statement that can make your code much more concise.
Its syntax is conditional statement ? <execute if true> : <execute if false>
. You can assign the return value of the expression to a variable.
age = 19
response = age < 18 ? "You still have your entire life ahead of you." : "You're all grown up."
puts response #=> "You're all grown up."
Loops
Loop
It’s an infinite loop that will keep going unless you specifically request for it to stop, using the break
command. Most commonly, break
is used with a condition, as illustrated in the example below.
i = 0
loop do
puts "i is #{i}"
i += 1
break if i == 13
end
While Loop
A while
loop is similar to the loop
loop except that you declare the condition that will break out of the loop up front.
i = 0
while i < 13 do
puts "i is #{i}"
i += 1
end
Until Loop
The until
loop is the opposite of the while
loop. A while
loop continues for as long as the condition is true, whereas an until
loop continues for as long as the condition is false.
i = 0
until i >= 10 do
puts "i is #{i}"
i += 1
end
Ranges
We can give Ruby ranges to loop:
(1..5) # inclusive range: 1, 2, 3, 4, 5
(1...5) # exclusive range: 1, 2, 3, 4
# We can make ranges of letters, too!
('a'..'d') # a, b, c, d
For Loop
for i in 0..5
puts "#{i} zombies incoming!"
end
Times Loop
Bonus of accessing the number it’s currently iterating through.
5.times do
puts "Hello, world!"
end
5.times do |number|
puts "Alternative fact number #{number}"
end
Upto and Downto Loops
5.upto(10) { |num| print "#{num} " } #=> 5 6 7 8 9 10
10.downto(5) { |num| print "#{num} " } #=> 10 9 8 7 6 5
Top comments (0)