Looping with Ruby Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

The ‘While’ Loop

A
The while loop executes while the condition is true.  Syntax:
while [condition] do   (do is optional)
    code
end
Example:
while a < 10
  print a
  a += 1
end

https://launchschool.com/books/ruby/read/loops_iterators#forloops

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

The ‘Until’ Loop

A
The until loop executes while a condition is false (until is becomes true
Syntax:
until conditional 
    code
end
Example:
until a == 10
  print a
  a += 1
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The ‘For’ Loop

A
The for loop is used to loop over a defined collection of elements (i.e. each value in a series or in an array)
Syntax:
for [new variable] in [some collection of elements]
    code
end
Examples:
x = [1, 2, 3, 4, 5]
for i in x do
  puts i
end

for num in 1..15
puts num
end

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

Inclusive and Exclusive Ranges

A

A Range represents an interval—a set of values with a beginning and an end.
An Inclusive Range uses two dots (..) and includes the last element, e.g. 1..5 => 1, 2, 3, 4, 5
An Exclusive Range uses three dots (…) and excludes the last element, e.g. 1…5 => 1, 2, 3, 4

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

The ‘Loop’ (Break) Loop

A
i = 20
loop do
  i -= 1
  print "#{i}"
  break if i <= 0
end

=> 191817161514131211109876543210

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

Next statement

A

The next keyword (reserved word) can be used to skip over certain steps in the loop. If you place the next reserved word in a loop, it will jump from that line to the next loop iteration without executing the code beneath it.

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

Next stmt examples; NOTE the use of END in the conditional

The examples produce the same result

A
for i in 0..5
   if i < 2 then
      next
   end  # <= focus on this end
   puts "Value of local variable is #{i}"
end

for i in 0..5
next if i < 2
puts “Value of local variable is #{i}”
end

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

Iterators

A

Iterators are methods that naturally loop over a given set of data and allow you to operate on each element in the collection.

Types: Each,

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

The .each Iterator

A
The .each method, which can apply an expression to each element of an object (such as an range, hash, or an array), one at a time.  Syntax and example below:
object.each do | variable_name | 
  # Do something 
end
array = [1,2,3,4,5]
array.each do |x|
  x += 10
  print "#{x}"
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The .times iterator

A

it can perform a task on each item in an object a specified number of times. Syntax and Example

t.times do |variable_name|  #variable not required
# code to be execute
end

5.times do
puts “Hello”
end

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

The .split Method

A

Takes in a string and returns an array. If we pass it a bit of text in parentheses, .split will divide the string wherever it sees that bit of text, called a delimiter. For example:
text.split(“,”) # splits the text at the commas

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