Assignment operators in Ruby are used to assign or update values to variables. The most common assignment operator is =
but others also exist, like +=
, -=
, *=
and /=
.
a = 1;a += 3;puts a; # Output: 4b = 4;b -= 2;puts b; # Output: 2num = 12;num *= 2;puts num; # Output: 24num /= 4;puts num; # Output: 6
To iterate over an array in Ruby, use the .each
method. It is preferred over a for
loop as it is guaranteed to iterate through each element of an array.
data = [3, 6, 9, 12]data.each do |num|puts "The number is: #{num}"end# Output:# The number is: 3# The number is: 6# The number is: 9# The number is: 12
In Ruby, the next
keyword is used within a loop to pass over certain elements and skip to the following iteration. It is useful for omitting elements that you do not wish to have iterated. next
is followed by an if
statement which defines which elements are to be skipped.
for i in 1..10next if i % 2 == 0puts iend# In this example, the next# keyword along with a shorthand# if statement is used to skip# over the even numbers in the sequence.# Output:# 1# 3# 5# 7# 9
Putting a block of code in a while
loop in Ruby will cause the code to repeatedly run the code as long as its condition is true
.
If the block of code doesn’t have a way for the condition to be changed to false
, the while
loop will continue forever and cause an error.
i = 1while i <= 3 doputs "Message number #{i}"i = i + 1end# Output:# Message number 1# Message number 2# Message number 3
To execute the same block of code a set a number of times in Ruby, use the times
method.
5.times { puts "Codecademy" }# Output:# Codecademy# Codecademy# Codecademy# Codecademy# Codecademy
In ruby, a sequence of integers can be demonstrated by a range. The range can be divided into an inclusive range where the last integer in the sequence is included and an exclusive range where the last integer is excluded.
# Inclusive(3..5).each do |i|puts iend# Output:# 3# 4# 5# Exclusive(3...5).each do |i|puts iend# Output# 3# 4
A loop
method can be used to run a block of code repeatedly in Ruby. Either use curly braces ({}
) or the do
/end
keyword combination to wrap the block of code that will be looped.
num = 1loop doputs "We are in the loop!"num += 1break if num > 3endputs "We have exited the loop!"# Output# We are in the loop!# We are in the loop!# We are in the loop!# We have exited the loop!
Putting a block of code inside an until
loop in Ruby will cause the code to run as long as its condition remains false
. It’s only when the condition becomes true
that the loop stops.
If the block of code doesn’t allow for a way for the condition to be changed to true
then the loop will continue forever and it will cause an error.
i = 1until i == 4 doputs "Message number #{i}"i = i + 1end# Output# Message number 1# Message number 2# Message number 3
A block of code can be repeated a set amount of times with the for
loop in Ruby.
for i in 1..3 doputs "Message number #{i}"end# Output# Message number 1# Message number 2# Message number 3