I was assuming Procs definitions where not able to use the & for the last parameter.
I’m happy with that solution. What I wanted was a way to pass a block to a Proc on call, and that allows me to do that.
Note aside: It is not easy (at least for me) documentation about this feature. Just found in the book “Programming Ruby 1.9 & 2.0” in Chapter 22.13 “Blocks, Closures, and Proc Objects”.
The reason behind is hinted by the variable name “defer”. In your example, what I wanted to do was to defer the calculation of 1, 2 and 3 until f is called. So the code would be:
l = ->(deferred_e){puts deferred_e.call}
def get_each(f)
f.call(lambda{1})
f.call(lambda{2})
f.call(lambda{3})
end
get_each l
The alternative I found nicer using the & as you suggested first is:
l = ->(&deferred_e){puts deferred_e.call}
def get_each(f)
f.call {1}
f.call {2}
f.call {3}
end
get_each l
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.