Loops and itterators Flashcards
how does the while loop look like?
i = 0
while i
how does the until loop look like?
i = 0 until i > 10 print i i = i + 1 end
how can you refactor this code i = i + 1 ?
i += 1
and it can be used with i -=1 and i *= 2 as well.
how does the for loop look like?
for num in 1..10
puts num
end
what is the different between 1..10 and 1…10 ?
the …»_space; does not include the last object in the range
the ..»_space; includes the last object in the range
how do you refactor a do end block?
{ } are used interchangeably for the do end block.
how does a loop iterator look like? and how to stop a loop?
i = 0 loop do i += 1 puts i break if i > 20 end
how to skip steps in a loop?
for i in 1..5
next if i % 2 == 0
print i
end
how does the each iterator look like?
object.each do |item| #do something end
object.each { |item| #do something }
how does the times iterator look like?
10.times { #do something }
a method that takes a string and returns an array.
split
“I am the guru”.split(“ “)
» [“I”, “am”, “the”, “guru”]