Yielding in an anonymous block

后端 未结 2 508
余生分开走
余生分开走 2021-01-03 01:56

I\'m not making sense of the following behavior (see also in this SO thread):

def def_test
  puts \'def_test.in\'
  yield if block_given?
  puts \'def_test.o         


        
2条回答
  •  盖世英雄少女心
    2021-01-03 02:20

    block_given? considers def scope, not lambda scope:

    def test
      l = lambda do
        yield if block_given?
      end
      l.call
    end
    
    test { puts "In block" }
    

提交回复
热议问题