Test Your Understanding!

Time to put on that thinking cap and flex that gray matter. Remember, in order to really understand the material, you should attempt to complete the following exercises. None of them should take too long, and you have my permission to peek at the solutions if you get stuck.

  1. Implement Array#map using Array#each:

     %w(look ma no for loops)​.map ​do​ |x|
      x.upcase
     end

    This should return ["LOOK", "MA", "NO", "FOR", "LOOPS"].

  2. Implement String#each_word:

     "Nothing lasts forever but cold November Rain"​.each_word ​do​ |x|
      puts x
     end

    This should output:

     Nothing
     lasts
     forever
     but
     cold
     November
     Rain
  3. It’s your turn to implement File.open. Start off with the Ruby documentation. The key here is to understand ...

Get Mastering Ruby Closures now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.