Ruby method Lambda block proc connection and difference-next break return

Source: Internet
Author: 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

  

 

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.