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;