DEV Community

Joshua Jones
Joshua Jones

Posted on

Loops & Iterators in Ruby

Image description

More operators: +=, -=, *=, /=

For: when you don't know how many times you need to loop
For the () puts ()
ex: for num in 1...10 puts num end
(...)= exclude the last digit
(..)= include the last digit

Loop: create basic but infinite loop.
Image description

Next: Can be used to skip over certain steps in the loop.
Image description

Array: my-array can store numbers in a variable
my_array = [1,2,3,etc]

.each: can apply an expression to each element of an object. You wrap in (| |)
Image description

Image description

.times: It performs a task on an item a specified number of times.
ex: 10.times { print "Chunky bacon!" }

until: Until a certain criteria is met

Using Loop iterator to print out the Ruby string 30 times:
Image description

using .times iterator to print Ruby 30 times:
Image description

redacted:
Image description

Top comments (0)