Blocks

a = 123

def test
  yield
end

# block parameters, scoped local to the block
test do |a|
  a = 456
end
puts a
#=> 123

# block local variable
test do |;a|
  a = 456
end
puts a
#=> 123

# override variables outside the block
test do
  a = 456
end
puts a
#=> 456
# yield with parameters and get the return values
def test
  a = 'test'
  a = yield a
  puts a
end

test do |a|
  puts a
  a = 'test2'
end
#=> test
#=> test2

Reference

results matching ""

    No results matching ""