ruby method lambda block proc 聯絡與區別 — next break return

來源:互聯網
上載者:User
$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

  

 

運行結果:

-------------lambda----------

next  in lamb()

next  in lamb()

next  in lamb()

-------at the end of lamb() : next  in lamb()

break in in lamb()

break in in lamb()

break in in lamb()

-------at the end of lamb() : break in in lamb()

return in in lamb()

return in in lamb()

return in in lamb()

-------at the end of lamb() : return in 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()

解釋下

Method :方法

lambda:匿名方法

block:程式碼片段

proc:有名block

Method:普通函數

lambda:匿名函數,內部的return 不會 退出上級 的代碼。

block:無名程式碼片段,內部的return 會 退出上級代碼。

proc:具名(有名)程式碼片段,內部的return 會 退出上級代碼。

內部關鍵字:

next   break   和   return 的區別

lambda運算式中 next :返回給調用它的函數,不會退出調用它的函數。

lambda運算式中 break:與next沒差別好像

lambda運算式中 return :與next沒差別好像

block運算式中 next : 

block運算式中 break: 

block運算式中 return :退出調用它的函數

proc運算式中 next :返回給調用它的函數。

proc運算式中 break:與next沒差別好像

proc運算式中 return :

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.