$res = "__"
def lamb(&lamb)
3.times do
puts $res=lamb.call
end
puts "-------at the end of lamb() : #{$res}"
end
def lamb_test
lamb { return puts " return in lamb() " }
puts "--------at the lamb_test() end"
end
def bl
3.times do
puts $res= yield
end
puts " ------at the end of bl() :#{$res}"
end
def bl_test
bl do return puts "use return in bl()" end
puts "-----at the end of test_test()"
end
def p(&pro)
3.times do
puts $res = pro.call
end
puts "-----at the end of p() : #{$res}"
end
def p_test
p do return puts "use return in p() of p_test()" end
puts "-----at the end of p_test()"
end
puts "-------------lambda----------"
nextt = lambda{ next "next in lamb()"; puts "behind next" }
breakk = lambda{ break "break in in lamb()";puts "behind break" }
ret = lambda{ break "return in in lamb()";puts "behind return" }
lamb(&nextt)
lamb(&breakk)
lamb(&ret)
lamb_test
puts "\n----------------block--------"
bl { next "next in bl()" ; puts "behind next" }
bl { break "break in bl() " ;puts "behind break" }
#bl { return "return in bl() " ;puts "behind return" }
bl_test
puts "\n-----------proc------------"
ne = Proc.new { next "next in p() "; puts "behind next" }
br = Proc.new { break "break in p() " ;puts "behind break" }
#retu = Proc.new { return "return in p() " ;puts "behind return" }
p(&ne)
#p(&br)
#p(&retu)
p_test
Running result:
------------- Lambda ----------
Next in lamb ()
Next in lamb ()
Next in lamb ()
------- At the end of Lamb (): Next in lamb ()
Break in lamb ()
Break in lamb ()
Break in lamb ()
------- At the end of Lamb (): Break In Lamb ()
Return in lamb ()
Return in lamb ()
Return in lamb ()
------- At the end of Lamb (): Return in lamb ()
Return in lamb ()
---------------- Block --------
Next in BL ()
Next in BL ()
Next in BL ()
------ At the end of BL (): Next in BL ()
Use return in BL ()
----------- Proc ------------
Next in P ()
Next in P ()
Next in P ()
----- At the end of P (): Next in P ()
Use return in P () of p_test ()
Explanation
Method: Method
Lambda: anonymous method
Block: code segment
Proc: famous Block
Method: Common Function
Lambda: anonymous function. Internal return does not exit the code of the upper-level.
Block: Unknown code snippet. Internal return will exit the upper-level code.
Proc: A named (famous) code snippet. The internal return will exit the superior code.
Internal keywords:
Differences between next break and return
In a Lambda expression, "Next" is returned to the function that calls it, and does not exit the function that calls it.
Break in Lambda expression: no difference from next
Return in Lambda expression: no difference from next
In the block expression, next:
Break in block expression:
Return in the block expression: exit the function that calls the block expression.
In the proc expression, "Next" is returned to the function that calls it.
Break In Proc expression: no difference from next
Return in Proc expression: