1 assert('Kernel.eval', '15.3.1.2.3') do
2 assert_equal(10) { Kernel.eval '1 * 10' }
3 assert_equal('aaa') { Kernel.eval "'a' * 3" }
17 Kernel.eval "c = a + c"
24 Kernel.eval 'lambda { c }.call'
31 Kernel.eval 'lambda { c = a + c }.call'
37 Kernel.eval 'def f(a); b=a+1; end'
42 assert('Kernel#eval', '15.3.1.3.12') do
43 assert_equal(10) { eval '1 * 10' }
46 assert('rest arguments of eval') do
47 assert_raise(ArgumentError) { Kernel.eval('0', 0, 'test', 0) }
48 assert_equal ['test', 'test.rb', 10] do
49 Kernel.eval('[\'test\', __FILE__, __LINE__]', nil, 'test.rb', 10)
53 assert 'eval syntax error' do
54 assert_raise(SyntaxError) do
59 assert('String instance_eval') do
61 obj.instance_eval{ @test = 'test' }
62 assert_raise(ArgumentError) { obj.instance_eval(0) { } }
63 assert_raise(ArgumentError) { obj.instance_eval('0', 'test', 0, 'test') }
64 assert_equal(['test.rb', 10]) { obj.instance_eval('[__FILE__, __LINE__]', 'test.rb', 10)}
65 assert_equal('test') { obj.instance_eval('@test') }
66 assert_equal('test') { obj.instance_eval { @test } }
68 assert_equal ['', o, o], o.instance_eval("[''].each { |s| break [s, o, self] }")
71 assert('Kernel.#eval(string) context') do
72 class TestEvalConstScope
73 EVAL_CONST_CLASS = 'class'
75 eval 'EVAL_CONST_CLASS'
78 obj = TestEvalConstScope.new
79 assert_raise(NameError) { eval 'EVAL_CONST_CLASS' }
80 assert_equal('class') { obj.const_string }
83 assert('BasicObject#instance_eval with begin-rescue-ensure execution order') do
86 order = [:enter_raise_hell]
89 self.instance_eval("raise 'error'")
99 hell_raiser = HellRaiser.new
100 assert_equal([:enter_raise_hell, :begin, :rescue, :ensure], hell_raiser.raise_hell)
103 assert('BasicObject#instance_eval to define singleton methods Issue #3141') do
104 foo_class = Class.new do
106 instance_eval "def baz; #{x}; end"
114 assert_equal(1){f1.baz}
115 assert_equal(2){f2.baz}
118 assert('Kernel.#eval(string) Issue #4021') do
119 assert_equal('FOO') { (eval <<'EOS').call }
123 assert_equal('FOO') {
127 do_eval(<<'EOS').call
134 assert('Calling the same method as the variable name') do
139 assert_equal("Hit!") { fuga = "Miss!"; eval "hoge.fuga" }
140 assert_equal("Hit!") { fuga = "Miss!"; -> { eval "hoge.fuga" }.call }
141 assert_equal("Hit!") { -> { fuga = "Miss!"; eval "hoge.fuga" }.call }
142 assert_equal("Hit!") { fuga = "Miss!"; eval("-> { hoge.fuga }").call }
145 assert('Access numbered parameter from eval') do
151 hoge.fuga(3) { _1 + eval("_1") }