Because that would execute this immediately when you define
double_then_square
, clearly not possible since we don't know that1
is getting passed in yet. Instead, you want something you can call, e.g., send a.call(1)
on. That is, we want to return aProc
.No, it should be doable. However, you will not be able to access your local variables, so you will have to define them in the method instead. Since it is a proper method, you will also call it directly instead of using
.call
. The code below works for me.
def double_then_square(*args)
square_it = Proc.new do |n|
n ** 2
end
double_it = Proc.new do |n|
n * 2
end
compose(double_it,square_it).call(*args)
end
double_then_square(1)
# 4