Ruby block, Proc, Lambda connection-difference, reproduced

Source: Internet
Author: User
The ruby block, Proc, and lambdablockblock are not objects. They are Ruby language features and are similar to closure ).

Example:

Def meth
Res = yield
"Block called returns # {res }"
End
Puts meth do next "next_value" end # block called returns next_value

Puts meth do break "break_value" end # break_vcowcuo error Oh alue

Def my

Meth do return "reutnr_value" end

End

Puts my () # return_value

The red part is block.

Block-to-next

The next [value] in the block causes the block to end and Returns [value] to the calling function.

Block break

The break [value] in the block causes the block to end, and the function that calls the block is also returned. The returned value is [value].

Block return

The return [value] in the block causes the block to end and the function that defines the block to return. The return value is [value].

 

Procproc is Ruby's object-oriented encapsulation of block.

Example:

Def meth (& proc)
Res = Proc. Call
"Proc called returns # {res }"
End
Puts meth do next "next_value" end # proc called returns next_value

Puts ETH do break "break_value" end # error

Def my

Meth do return "reutnr_value" end

End

Puts my () # return_value

The red part is Proc.

Proc-next

In Proc, next [value] causes proc to end and Returns [value] To the called function.

Block break

Break cannot be used in Proc, which causes an exception.

Block return

The return [value] In Proc results in the end of Proc and the return of the function defining Proc. the return value is [value].

 

Lambdalambda is Ruby's further encapsulation of Proc.

Example:

Def meth (& proc)
Res = Proc. Call
"Lambda called returns # {res }"
End

Def my

Meth (& Lambda do return "reutnr_value" end)

End

Puts my () # Lambda called returns return_value

The red part is lambda.

The difference between Lambda and proc is that the return in Lambda causes the lambda function to end and returns the function that calls lamdad (Proc and block are both removed from their defined functions)

 

Summary of block, Proc, and Lambda differences
  • The block uses next to return from itself, break to return from the function that calls it, And returnc to return from the function that defines it;
  • Proc is an object-oriented encapsulation of blocks and does not support breaky usage;
  • Lambda is the encapsulation of Proc. Return in Lambda is equivalent to next;
Blockblock is not an object and is a language feature of Ruby. It is similar to closure ).

Example:

Def meth
Res = yield
"Block called returns # {res }"
End
Puts meth do next "next_value" end # block called returns next_value

Puts meth do break "break_value" end # break_vcowcuo error Oh alue

Def my

Meth do return "reutnr_value" end

End

Puts my () # return_value

The red part is block.

Block-to-next

The next [value] in the block causes the block to end and Returns [value] to the calling function.

Block break

The break [value] in the block causes the block to end, and the function that calls the block is also returned. The returned value is [value].

Block return

The return [value] in the block causes the block to end and the function that defines the block to return. The return value is [value].

 

Procproc is Ruby's object-oriented encapsulation of block.

Example:

Def meth (& proc)
Res = Proc. Call
"Proc called returns # {res }"
End
Puts meth do next "next_value" end # proc called returns next_value

Puts ETH do break "break_value" end # error

Def my

Meth do return "reutnr_value" end

End

Puts my () # return_value

The red part is Proc.

Proc-next

In Proc, next [value] causes proc to end and Returns [value] To the called function.

Block break

Break cannot be used in Proc, which causes an exception.

Block return

The return [value] In Proc results in the end of Proc and the return of the function defining Proc. the return value is [value].

 

Lambdalambda is Ruby's further encapsulation of Proc.

Example:

Def meth (& proc)
Res = Proc. Call
"Lambda called returns # {res }"
End

Def my

Meth (& Lambda do return "reutnr_value" end)

End

Puts my () # Lambda called returns return_value

The red part is lambda.

The difference between Lambda and proc is that the return in Lambda causes the lambda function to end and returns the function that calls lamdad (Proc and block are both removed from their defined functions)

 

Summary of block, Proc, and Lambda differences
  • The block uses next to return from itself, break to return from the function that calls it, And returnc to return from the function that defines it;
  • Proc is an object-oriented encapsulation of blocks and does not support breaky usage;
  • Lambda is the encapsulation of Proc. Return in Lambda is equivalent to next;
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.