Ruby called a collection of blocks -


assume have built array of callable objects doing

callables = [] callables << block1 callables << block2 callables << block3 callables << block4 

later want call these blocks. understand can do

callables.each { |block| block.call } 

but wondering if make simpler calling like

callables.each :call 

i have tried code above got argumenterror. ruby support kind of syntax?

you should try :

callables.each &:call 

array#each don't accept arguments. that's why, when write callables.each :call, :call symbol passing method each argument. when prefixed :call &, each knows giving block argument, work.


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -