- Review the process of debugging code
- Review common iterators introduced in the previous lessons.
You're nearly to the end of the Ruby Prework content. Congratulations on getting this far! The next few labs will require you to apply everything you've learned so far. To help you prepare beforehand, this lesson includes both a quiz on Enumerables and a video review on debugging.
The video below walks through the process of debugging code with Pry using the upcoming Hashketball lab. Although you haven't seen this lab yet, the concepts reviewed are applicable to all of the Ruby labs you'll be working on going forward. Check out the previous Using Pry lesson to review Pry further.
<iframe width="560" height="315" src="https://www.youtube.com/embed/uv6fvdKXAnM" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>Try using Pry in the next lab!
???
?: Using .each
Let's try out the enumerator methods we just learned. Refer back to the
previous lessons to help you pass this challenge. Below, we have a variable,
lunch_menu
, set equal to an Array
of lunch menu items.
Since you're super-hungry and super-excited about lunch, use the .map
method
to enumerate over the Array
and append an "!"
("exclamation mark") to each
menu item.
lunch_menu = ["pizza", "sandwich", "sushi", "soup", "salad"]
Which piece of code will achieve the desired result?
( )
lunch_menu.reduce{|memo, item| memo << item }
(x)
lunch_menu.map{|item| "#{item}!" }
( )
lunch_menu.reduce{|item, memo| memo += item }
( )
lunch_menu.map{|item| (item, "!") }
?: Using .collect
Below we have a variable, nums
, set equal to an Array
of numbers. Enumerate
over the Array
and return a new Array
of the squares of those numbers.
nums = [1, 2, 3, 4]
Which piece of code will achieve the desired result?
(x)
nums.collect { |n| n * n }
( )
nums.collect do |n|
n + n
end
( )
nums.collect { |n| nn }
( )
nums.collect do |n|
n
end
?: Using .select
Below we have a variable, odds_and_evens
, set equal to an Array
of numbers.
Use the .select
enumerator to iterate over the Array
and return any even
numbers. This requires checking if a number is even. If you're unsure how to do
that, reference the Ruby Documentation for Integer
or try a Google search!
odds_and_evens = [1, 3, 2, 18, 5, 10, 24]
Which piece of code will achieve the desired result?
( )
odds_and_evens.select do |n|
n / 2
end
(x)
odds_and_evens.select do |n|
n % 2 == 0
end
( )
odds_and_evens.select do |n|
n.odd?
end
( )
odds_and_evens.select do |n|
n + 2
end
?: Using .find
Below we once again have a variable, odds_and_evens
, set equal to an Array
of
numbers. This time, use the .find
method to iterate over the Array
and return
only the first Array
element that is odd.
odds_and_evens = [2, 3, 2, 18, 5, 10, 24]
Which piece of code will achieve the desired result?
( )
odds_and_evens.find do |num|
num.even?
end
( )
odds_and_evens.find do |num|
num / 2
end
( )
odds_and_evens.find do |num|
num
end
(x)
odds_and_evens.find do |num|
num.odd?
end
?: Using include?
Below we have a variable, famous_cats
, set equal to an Array
of famous
cats. Use the .include?
method to check and see if the Array
includes the
string "Maru"
.
famous_cats = ["Maru", "Lil Bub", "Grumpy Cat"]
Which piece of code will achieve the desired result?
(x)
famous_cats.include?("Maru")
( )
famous_cats.include?
( )
famous_cats.include?(true)
???
View Enumerator Coding Challenge on Learn.co and start learning to code for free.