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

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -