Chapter 3
-
The answer is exactly the one covered in the chapter:
class Symbol def to_proc proc { |obj, args| obj.send(self, *args) } end end -
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.
-
The Proc#lambda? method.
-
Both come from the Proc class.
-
join_2. Lambdas ...
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.