| 1 |
assert "Proc#binding" do |
| 2 |
block = ->(i) {} |
| 3 |
a, b, c = 1, 2, 3 |
| 4 |
bind = block.binding |
| 5 |
assert_equal([:a, :b, :bind, :block, :c], bind.local_variables.sort) |
| 6 |
assert_equal(1, bind.local_variable_get(:a)) |
| 7 |
assert_equal(5, bind.eval("b + c")) |
| 8 |
bind.local_variable_set(:x, 9) |
| 9 |
assert_equal(9, bind.local_variable_get(:x)) |
| 10 |
end |
| 11 |
|
| 12 |
assert("Binding#source_location after Proc#binding") do |
| 13 |
skip unless -> {}.source_location |
| 14 |
|
| 15 |
block, source_location = -> {}, [__FILE__, __LINE__] |
| 16 |
assert_equal source_location, block.binding.source_location |
| 17 |
end |
| 18 |
|
| 19 |
assert "Proc#binding and .eval from C" do |
| 20 |
bind = proc_in_c.binding |
| 21 |
assert_nothing_raised { bind.eval("self") } |
| 22 |
end |