Convert Lambda to Proc
Here's a workaround that wraps a call to Lambda or method in Proc and uses splat to handle any number of arguments:
def lambda_to_proc(lambda) Proc.new do |*args| diff = lambda.arity - args.size diff = 0 if diff.negative? args = args.concat(Array.new(diff, nil)).take(lambda.arity) lambda.call(*args) end end
This will always work regardless of the number of arguments passed; Additional arguments will be removed, and nil will replace the missing arguments.
Example:
Note. There is no direct way to convert a lambda or method call to proc. This is just a workaround, and obviously slower than the real deal (due to wrapping one call in another).
Sheharyar
source share