Issue #9777 has been updated by Myron Marston.


We have a need for this feature in RSpec.  Specifically, for some of the new features in RSpec 3, we use `Method#parameters` and `Proc#parameters`.  However, for procs, `parameters` does not distinguish between args with defaults and args without:

~~~
irb(main):001:0> Proc.new { |a| }.parameters
=> [[:opt, :a]]
irb(main):002:0> Proc.new { |a=1| }.parameters
=> [[:opt, :a]]
~~~

I understand why, because with procs you can call it without providing the args so all args are optional and it's essentailly like `|a=nil|`.  Lambdas, on the other hand, do distinguish:

~~~
irb(main):003:0> lambda { |a| }.parameters
=> [[:req, :a]]
irb(main):004:0> lambda { |a=1| }.parameters
=> [[:opt, :a]]
~~~

I attempted to use some code like @schneem's gem to convert a proc to a lambda for the purpose of looking at the parameters (but, importantly, not to call the lambda).  Unfortunately, we ran into a ruby bug (#9967) and had to back it out.

It would be nice for an officially supported solution to convert a proc to a lambda. I can understand the issues with calling a converted lambda, but simply using the converted lambda to get more precise info about the block parameters seems more reasonable and safe.

----------------------------------------
Feature #9777: Feature Proposal: Proc#to_lambda
https://bugs.ruby-lang.org/issues/9777#change-47317

* Author: Richard Schneeman
* Status: Feedback
* Priority: Normal
* Assignee: 
* Category: core
* Target version: current: 2.2.0
----------------------------------------
Currently different block objects such as a lambda can be converted into to a proc: http://www.ruby-doc.org/core-1.9.3/Proc.html#method-i-to_proc

However you cannot turn a Proc instance into a lambda. Since a Proc and lambda behave differently sometimes you may want to convert between the two functionalities. One example is a `return` inside of the block. In a lambda the `return` keyword exits the closure, in a Proc the `return` keyword raises an exception.

There is currently no implementation standard way to convert a Proc to a lambda. I made a gem that makes this easier: https://github.com/schneems/proc_to_lambda but it seems overkill.

If MRI introduces a `to_lambda` method on Proc then we can standardize on an interface for this behavior. This question on stack overflow has been upvoted many times: http://stackoverflow.com/questions/2946603/ruby-convert-proc-to-lambda. I think other Ruby developers would like this behavior supported by Ruby core.





-- 
https://bugs.ruby-lang.org/