Loops and itterators Flashcards

1
Q

how does the while loop look like?

A

i = 0

while i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how does the until loop look like?

A
i = 0
until i > 10
  print i
  i = i + 1
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how can you refactor this code i = i + 1 ?

A

i += 1

and it can be used with i -=1 and i *= 2 as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how does the for loop look like?

A

for num in 1..10
puts num
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what is the different between 1..10 and 1…10 ?

A

the …&raquo_space; does not include the last object in the range

the ..&raquo_space; includes the last object in the range

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how do you refactor a do end block?

A

{ } are used interchangeably for the do end block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

how does a loop iterator look like? and how to stop a loop?

A
i = 0
loop do 
  i += 1
  puts i
  break if i > 20
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to skip steps in a loop?

A

for i in 1..5
next if i % 2 == 0
print i
end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how does the each iterator look like?

A
object.each do |item| 
  #do something
end

object.each { |item| #do something }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

how does the times iterator look like?

A

10.times { #do something }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

a method that takes a string and returns an array.

A

split
“I am the guru”.split(“ “)
» [“I”, “am”, “the”, “guru”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly