- Please go through the preread content carefully before Wednesday's class at 9 AM. Watch, read, and practice with cohort-mates!
- Content marked (optional) is for review or extra learning.
- Read about the flipped classroom philosophy here (optional): https://en.wikipedia.org/wiki/Flipped_classroom
- There's a lot of material here, so budget time appropriately today. Think of this as another challenge.
- Review iteration conceptually
- Apply iteration to linear search
- Learn and practice recursion syntax and recursive algorithms
- Practice iteration and looping syntax in Ruby
- Learn the important methods in the Enumerable Module with a focus on Arrays and Hashes
These videos target the C programming language, which you are not expected to know. Don't get hung up on the syntax and instead focus on the concepts. These cover learning goals 1-3. Come to class with questions!
- Linear Search (3.5 minutes): https://www.youtube.com/watch?v=CX2CYIJLwfg
- Recursion (first 6.5 minutes only, note that Ruby does not have a "void" return type): https://www.youtube.com/watch?v=t4MSwiqfLaY
- Binary Search (first 5 minutes only): https://www.youtube.com/watch?v=D5SrAga1pno
Practice the code snippets from the following post in your terminal using the irb command in iTerm. Here are the loops in the order they appear; feel free to skip optional ones. You don't need to read every word of this blog post, just cover the keywords listed below and practice in irb. This covers learning goal 4.
loop, break, next, redo(the info in the article for retry is wrong, do not read)whileanduntilwhileanduntilusingbeginsyntaxforeach- skip
each_index, that method has been removed from Ruby as of 2.2 and possibly before) times(optional, not really used)upto, step(optional, not really used)
[Ruby Loops (30 minutes)] (http://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators/)
Below is the documentation page for the Enumerable module. Both Array and Hash implement the Enumerable methods. Each method name is prefixed with a pound sign as in Enumerable#first. This means first is an instance method and works on objects which are of classes that include the Enumerable module (classes like Array and Hash).
arr = Array.new
arr << 10
arr.first # this line returns 10
Look into the following methods as described in the link in the bottom of this section and type the code examples in irb. Remember that the # sign means that the method is an instance method. If there is a / such as #collect/#map, this means that the two methods #collect and #map are EXACTLY THE SAME.
- Information:
#count,#first,#include?,#none?,#maxand#min,#max_byand#min_by - Transformation:
#collect/#map,#detect/#find,#select,#inject/#reduce,#reject,#sort,#sort_by,#to_a,#to_h(only in Ruby 2.1+, don't freak out if it doesn't work),#group_by - Iteration:
#each_with_index,#reverse_each
The documentation language is quite dry and difficult to understand. You will get the most from practicing the examples in irb. This covers learning goal 5.