OILS / demo / survey-closure.rb View on Github | oilshell.org

23 lines, 16 significant
1
2def create_context(x)
3 y = 20
4 binding
5end
6
7binding = create_context(10)
8puts binding
9puts binding.class
10
11vars = binding.eval("local_variables")
12puts vars
13puts vars.class
14
15# Why doesn't this work?
16#myproc = proc { puts "x: #{x}, y: #{y}" }
17
18# You need this longer thing
19myproc = proc { puts "x: #{binding.eval("x")}, y: #{binding.eval("y")}" }
20
21# Execute the block in the context
22binding.instance_exec(&myproc)
23