-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closures in Ruby #1
Comments
Referenceadditional references |
Closed
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Closure
Definition
Application
Closures in Ruby
Blocks
A code block is a set of Ruby statements and expressions between braces or a do/end pair.
block arguments
Blocks argument lists are very similar to method argument lists:
Proc Objects
Ruby’s blocks are chunks of code attached to a method. Blocks are not objects, but they can be converted into objects of class Proc.
Proc.new
, again associating it with a blockObject#lambda
, associating a block with the call.->
syntax.The first two styles of Proc object are identical in use. We’ll call these objects raw procs. The third and fourth styles, generated by lambda and ->, add some functionality to the Proc object, as we’ll see in a minute. We’ll call these objects lambdas.
Here’s the big thing to remember: raw procs are basically designed to work as the bodies of control structures such as loops. Lambdas are intended to act like methods. So, lambdas are stricter when checking the parameters passed to them, and a return in a lambda exits much as it would from a method.
calling a Proc
You call a proc by invoking its methods
call
,yield
, or[]
.name.(args...)
. This is mapped internally intoname.call(args...)
.Procs, break, and next
Within both raw procs and lambdas, executing
next
causes the block to exit back to the caller of the block. The return value is the value (or values) passed tonext
, ornil
if no values are passed.Within a raw proc, a break terminates the method that invoked the block. The return value of the method is any parameters passed to the break.
Return and Blocks
A return from inside a raw block that’s still in scope acts as a return from that scope. A return from a block whose original context is no longer valid raises an exception (LocalJumpError or ThreadError depending on the context).
The following example shows a return failing because the context of its block no longer
exists
And here’s a return failing because the block is created in one thread and called in another:
This is also true if you create the raw proc using Proc.new.
A lambda behaves more like a free-standing method body: a return simply returns from the block to the caller of the block:
Because of this, if you use
Module#define_method
, you’ll probably want to pass it a proc created usinglambda
, notProc.new
, because return will work as expected in the former and will generate aLocalJumpError
in the latter.Blocks and Closures
A block is a closure:
The method
n_times
returns aProc
object that references the method’s parameter,thing
. Even though that parameter is out of scope by the time the block is called, the parameter remains accessible to the block. This is called aclosure
—variables in the surrounding scope that are referenced in a block remain accessible for the life of that block and the life of any Proc object created from that block.Reference
Here list all the references
The text was updated successfully, but these errors were encountered: