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 are strict with arity.
Both. 1 + 2 + nil will raise an error.