문제

I'm trying to understand how this iterator works:

def two_times          
    yield
    yield
end

two_times {puts "Hello"}

Does the second part call the method and then yield calls {puts "Hello"}?

도움이 되었습니까?

해결책

Essentially, yes. The last line invokes the method two_times and passes in a block whose code prints out Hello. Each time the method executes yield, the block is executed.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top