ruby - What is the yield for? -


this question has answer here:

what purpose of yield in ruby? explain ? don't understand yield does:

def variable(&block)     puts 'here goes:'     case block.arity         when 0             yield         when 1             yield 'one'         when 2             yield 'one', 'two'         when 3             yield 'one', 'two', 'three'     end     puts 'done!' end 

you can use yield implicitly call block. defining call block if there's block given. example:

def test    puts "you in method"    yield    puts "you again method"    yield end test {puts "you in block"} 

that results in

you in method in block again method in block 

hope helps!


Comments