Issue #7314 has been updated by Andrew Vit.


Use case: a stored block gets called with a "target" as first parameter, plus optional arguments. If the target object is an array, then the behaviour is unpredictable:

~~~
lm = lambda { |target, *options| puts target.inspect, options.inspect }
pr = proc   { |target, *options| puts target.inspect, options.inspect }

lm.call([1,2], 'a')  #=> [1, 2], ["a"]
pr.call([1,2], 'a')  #=> [1, 2], ["a"]

lm.call([1,2])  #=> [1, 2], []
pr.call([1,2])  #=> 1, [2]
~~~

Note how calling the block with only a single array destructures the "target" argument. 
If we need to avoid destructuring, a lambda does the right thing, so converting it would be helpful.

----------------------------------------
Feature #7314: Convert Proc to Lambda doesn't work in MRI
https://bugs.ruby-lang.org/issues/7314#change-56770

* Author: Richard Schneeman
* Status: Assigned
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=begin

I have code where I need to convert a proc to a lambda (i need to be able to return out of the block). I would expect that passing a proc into a lambda to return a lambda. When I run this code on MRI i do not get the result I would expect

 my_proc = proc { |x| x }
 my_lambda = lambda &my_proc
 my_lambda.lambda?

The result is (({false})) but I would expect it to be  (({true}))

There is currently a way to turn a proc into a lambda in MRI like this: 

  def convert_to_lambda &block
    obj = Object.new
    obj.define_singleton_method(:_, &block)
    return obj.method(:_).to_proc
  end

But this feels like a hack, and is not supported across other implementations. I would expect that passing a proc into a lambda to return a lambda, I believe it is a bug.

=end





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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>