ruby - Why are block variables optional? -
non-block variables obligatory:
def foo arg; end foo # => argumenterror in order allow optional arguments, default value has supplied:
def foo arg = nil; end foo # => nil but block variables optional:
def foo █ end foo # => nil without default value, absent block variable assigned nil, , in fact, block variables cannot take default values:
def foo &block = ->{puts :foo}; end # => syntax error on contrary, method can take block not required in definition:
def foo; end foo{puts :foo} # => nil what advantage of block variables being designed opposed non-block variables?
there wouldn't point throwing error when no block given, since method uses either yield or block.call throw error anyway if there no block. there wouldn't point supplying default block, since default code executed can written method (which lot neater writing inline arguments).
Comments
Post a Comment