From: "Eregon (Benoit Daloze)" <noreply@...>
Date: 2021-12-03T11:52:52+00:00
Subject: [ruby-core:106453] [Ruby master Feature#17721] Proc.new should be able to contruct a lambda

Issue #17721 has been updated by Eregon (Benoit Daloze).


There is `Kernel.send(lambda ? :lambda : :proc) { ... }` if you really really need this.
But it's probably slow and Ruby implementations have troubles to optimize this as they have no idea when parsing if the block is for a proc or lambda.

Of course the more sensible thing would be:
```ruby
class Proc
  def negate
    if lambda?
      lambda do |*args, &block|
        not self.(*args, &block)
      end
    else
      proc do |*args, &block|
        not self.(*args, &block)
      end
    end
  end
end
```
That has a significant advantage that a given block only has proc or lambda semantics but not both (and so using e.g., `break` is possible).
And also from looking at the backtrace you can know which it was.

I don't think it matters if `negate` e.g. always returns a lambda in this case (the caller can only observe the difference though `Proc#lambda?` and that shouldn't matter), so it should just be:
```ruby
class Proc
  def negate
    -> (*args, &block) do
      not self.(*args, &block)
    end
  end
end

```

As discussed extensively in related tickets, it's a very bad idea to convert from proc to lambda or vice-versa, but at least this issue only suggests a polymorphic create with a literal block.

So the important question here is why do you need to preserve whether the source Proc was a lambda or not?

----------------------------------------
Feature #17721: Proc.new should be able to contruct a lambda
https://bugs.ruby-lang.org/issues/17721#change-95110

* Author: bughit (bug hit)
* Status: Feedback
* Priority: Normal
----------------------------------------
since procs and lambdas are of the same type, as long as Proc::new exists, it should be able to create either.

```ruby
class Proc
  def augment
    self.class.new lambda? do 
      call
    end
  end
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>