Chapter 3

  1. The answer is exactly the one covered in the chapter:

     class​ Symbol
     def​ to_proc
      proc { |obj, args| obj.send(self, *args) }
     end
     end
  2. Since we’re interested in adding behavior to object initialization, it therefore makes sense to implement to_proc within the Class class. Here’s a possible implementation:

     class​ Class
     def​ to_proc
      proc { |args| ​new​(*args) }
     end
     end

    Since we’re creating objects with arrays, each array element is treated as a single object. Therefore, the proc takes a single argument.

    Next, we use the splat operator to convert the array into method arguments, and pass it into new, which then calls the initializer.

  3. The Proc#lambda? method.

  4. Both come from the Proc class.

  5. join_2. Lambdas are strict with arity.

  6. Both. 1 + 2 + nil will raise an error.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset