Test Your Understanding!

Now it’s time to work that gray matter! Do the following exercises to be sure you have grasped the concepts that were presented in this chapter. Don’t skip this!

  1. What is the definition of a closure?

  2. Identify the free variable in the following:

     def​ is_larger_than(amount)
      lambda ​do​ |a|
      a > amount
     end
     end

    Here’s an example run:

     >>​ larger_than_5 = is_larger_than(5)
     
     >>​ larger_than_5.call(4)
     =>​ ​false
     
     >>​ larger_than_5.call(5)
     =>​ ​false
     
     >>​ larger_than_5.call(6)
     =>​ ​true
  3. You work in a music store and you’ve been tasked with writing a miniature database to store artists and album titles. The database should be able to insert, delete, and list entries, but you cannot use objects ...

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.