Is there a short-cut for the code below that will pass only the expected number of arguments (the "arity") to the lambda (and ignore the rest)?
args = [:p1, :p2, :p3, :p4, :p5, :p6]
expected_args = args[0...the_lambda.arity]
value = the_lambda.call(*expected_args)
The big picture is that I'm writing a Ruby program where the user can write a Ruby config file that contains lambdas. I'd like the user to be able to write these lambdas using only the parameters needed (without having to do "extra" typing like including parameters that won't be used).
With the code above the user could write
my_override = ->(a) {....}
or
my_override = ->(a, b, c) { ....}
without the compiler complaining. (Part of the idea here is that when teaching new users, we wouldn't even need to mention the fact that there is more than one or two parameters. We could simply "save" those extra features to be revealed to more advanced users.) The end goal is to make the writing of the lambdas as simple as possible -- which means avoiding having elements of the user-written code that do not directly contribute to the functioning of the lambda.
Generally speaking I agree with the comments of using a
Procor an optionsHashbut you can facilitate your request by wrapping the "user lambda" and the argument passing into a method call like soWhile I would recommend encapsulatimg this functionality in a Class (Working Example), this will gather the arguments and keyword arguments for the lambda and then invoke the user lambda with those arguments by extracting them from the arguments passed to the wrapper.
Example: