Вопрос

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