Loops and iterators Flashcards
What is a While loop?
While something is true the code will run, there is a danger of infinite loops in these cases, make sure to add a increment.
Ex i= i+1
What is the each loop?
The each loop is an iterator
that goes through an object and executes the code on each single object.
What is the one line each iterator?
arr.each { | x | do_something x }
What is the syntax for the each iterator?
arr.each do |x|
do_something x
end
What are nested iterators?
nested iterators are used when you want to extract and operate on hashes and arrays with multiple objects.
What is the select method in ruby?
The select method is a method that automatically iterates through a collection and retrieves the value you want.
How do you use the select method in ruby?
There are three ways to use the select method in ruby: - something.select do |x| some_code end” - something.select { |x| x.some_code} - something.select(&:some_code)