From 568c5a8193006d12a47befaf493d1bbd76a5a5cd Mon Sep 17 00:00:00 2001 From: ko1 Date: Fri, 9 Aug 2013 09:51:00 +0000 Subject: * proc.c: add Binding#local_variable_get/set/defined? to access local variables which a binding contains. Most part of implementation by nobu. * test/ruby/test_proc.rb: add a tests for above. * vm.c, vm_core.h (rb_binding_add_dynavars): add a new function to add a new environment to create space for new local variables. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42464 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/ruby/test_proc.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'test/ruby/test_proc.rb') diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb index 2db9108534..1bc360c9e8 100644 --- a/test/ruby/test_proc.rb +++ b/test/ruby/test_proc.rb @@ -1207,4 +1207,38 @@ class TestProc < Test::Unit::TestCase bug8345 = '[ruby-core:54688] [Bug #8345]' assert_normal_exit('def proc; end; ->{}.curry', bug8345) end + + def get_binding if: 1, case: 2, when: 3, begin: 4, end: 5 + a = 0 + binding + end + + def test_local_variable_get + b = get_binding + assert_equal(0, b.local_variable_get(:a)) + assert_raise(NameError){ b.local_variable_get(:b) } + + # access keyword named local variables + assert_equal(1, b.local_variable_get(:if)) + assert_equal(2, b.local_variable_get(:case)) + assert_equal(3, b.local_variable_get(:when)) + assert_equal(4, b.local_variable_get(:begin)) + assert_equal(5, b.local_variable_get(:end)) + end + + def test_local_variable_set + b = get_binding + b.local_variable_set(:a, 10) + b.local_variable_set(:b, 20) + assert_equal(10, b.local_variable_get(:a)) + assert_equal(20, b.local_variable_get(:b)) + assert_equal(10, b.eval("a")) + assert_equal(20, b.eval("b")) + end + + def test_local_variable_defined? + b = get_binding + assert_equal(true, b.local_variable_defined?(:a)) + assert_equal(false, b.local_variable_defined?(:b)) + end end -- cgit v1.2.3