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:
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:
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?