Agent M. wrote in post #1006092:
Hello!
When inside a block, do I have access to a local integer counter
What do you mean by ‘local’?
[1, 2, 3]. each do |num|
counter = 1
puts counter #yes, puts() can see the local variable counter
end
–output:–
1
1
1
Other locations for counter can easily be tested:
counter = 0
[1, 2, 3]. each do |num|
counter += 1
puts counter
end
–output:–
1
2
3
Blocks ‘close’ over the variables that can be seen at the time the block
is created. Thereafter, the block consults those variables for their
values. Here is an example:
x = 1
y = 3
my_function = Proc.new {|arg| (x + y) * arg}
#def’s create a new scope, cutting off the visibility of the outer scope
def do_stuff(func)
x = 10 #this x variable is not the same variable as the outer x
y = 30 #this y variable is not the same variable as the outer y
func.call(2)
end
result = do_stuff(my_function)
puts result
–output:–
8
Compare to this:
x = 1
y = 3
my_function = Proc.new {|arg| (x + y) * arg}
#closes over outer x, y variables
x = 10 #changes outer x
y = 30 #changes outer y
result = my_function.call(2)
puts result
–output:–
80
If you ever have problems with a block getting the wrong values for
variables, look back in your code to where the block was created(i.e.
where it was
written in your code by you), and write down all the variables(and their
values) that the block
can see at that point in your code. Those are the variables that the
block sees when it executes.