ruby - Should I specify &block argument in def? -
in ruby, better (style?) specify method takes &block
or not?
the choice merely seems matter of style long method body contains yield
.
for example, given:
def abc1(a, c) puts yield puts c end def abc2(a, c, &block) puts yield puts c end
the following 2 calls:
abc1('a', 'c') { puts 'b' } abc2('a', 'c') { puts 'b' }
each print , return same things:
a b c => nil
so, if it's matter of style, what's convention (or better style)?
with current code first 1 better. while using yield
then, no need use &block
, implicit. yes, 1 thing remind, have pass block while using yield
, if not there error. although error can handled using block_given?
.
ruby's yield
statement gives control user specified block method's body. so, if using again &block
redundant, don't need use it.
Comments
Post a Comment