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

Chapter 4

  1.  class​ Lax < Enumerator
     # previously defined methods go here
     
     def​ select(&block)
      Lax.new(self) ​do​ |yielder, val|
      yielder << val ​if​ block.call(val)
     end
     end
     
     end
  2. This one is slightly more challenging, but resembles Lax#take:

     class​ Lax < Enumerator
     
     # previously defined methods go here
     
     def​ drop(n)
      dropped = 0
      Lax.new(self) ​do​ |yielder, val|
     if​ dropped < n
      dropped += 1
     else
      yielder << val
     end
     end
     end
     
     end

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