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!

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

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