O'Reilly logo

Mastering Ruby Closures by Benjamin Tan Wei Hao

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required