I want to convert a proc that shows lambda behavior (argument checking) to one that doesn't. The following is a very far-fetched example, but it should get the point:
The goal is to create a DSL that looks something like this:
NumberSeries.perform do
add first_series: -> { natural_numbers.take(10) },
second_series: -> { fibonacci_numbers.take(10) }
end
Note that natural_numbersand fibonacci_numbersare not passed as arguments to the DSL. The implementation addlooks something like this:
NaturalNumbersFibonacciNumbers = Struct.new(:natural_numbers, :fibonacci_numbers)
FAMOUS_NUMBER_SERIES = NaturalNumbersFibonacciNumbers.
new(natural_numbers, fibonacci_numbers)
def add(first_series:, second_series:)
first_numbers = FAMOUS_NUMBER_SERIES.instance_eval(&first_series)
second_numbers = FAMOUS_NUMBER_SERIES.instance_eval(&second_series)
first_numbers.zip(second_numbers).map { |x, y| x + y }
end
Now, if I replaced ->with procin the DSL, it will work. However, keeping lambdas, I would get
ArgumentError: wrong number of arguments (1 to 0)
as BasicObject#instance_evalgives self for lambda, but lambda does not expect arguments.
I do not want to use Fiddlefor obvious reasons.