Block Scope
...or, how closures break encapsulation (in a good way)
Example of Block Scope
def twice
yield
yield
end
def flatter
message = "you are great"
twice do
puts "#{message}!"
end
end
message is in the scope of flatter, not twice
locals, function parameters, block parameters
def twice_with word
yield word
yield word.upcase
end
def flatter person
message = "you are great"
twice_with(person) do |name|
puts "#{message}, #{name}!"
end
end
flatter "Alex"
-
person is a parameter of flatter
-
message is a local variable of flatter
-
name is a parameter of the block
-
word is a parameter of twice_with
Q: Which variables are available inside the block?