Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 157 lines (134 sloc) 2.767 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
assert "42", "p eval('40 + 2')"
assert "42", "def foo; 42; end; p eval('foo')"
assert "42", "x = 40; p eval('x + 2')"
assert "42", "x = 0; eval('x = 42'); p x"

assert ":ok", "eval('x = 42'); begin; p x; rescue NameError; p :ok; end"

assert "42", %q{
def foo(b); x = 42; eval('x', b); end
p foo(nil)
}

assert "42", %q{
def foo(b); x = 43; eval('x', b); end
x = 42
p foo(binding)
}

assert "42", %q{
def foo(b); x = 0; eval('x = 42', b); end
x = 1
foo(binding)
p x
}

assert "42", %q{
def foo; x = 123; bar {}; x; end
def bar(&b); eval('x = 42', b.binding); end
p foo
}

assert '42', %q{
def foo; x = 42; proc {}; end
p = foo; eval('p x', p.binding)
}

assert "42", %q{
class Foo;
def foo; 42; end;
def bar(s, b); eval(s, b); end;
end
def foo; 123; end
Foo.new.bar('p foo', nil)
}

assert "42", %q{
class Foo;
def foo; 43; end;
def bar(s, b); eval(s, b); end;
end
def foo; 42; end
Foo.new.bar('p foo', binding)
}

assert "42", "class A; def foo; @x; end; end; x = A.new; x.instance_eval { @x = 42 }; p x.foo"

assert "42", "b = binding; eval('x = 42', b); eval('p x', b)"

assert ":ok", "module M; module_eval 'p :ok'; end"
assert ":ok", "module M; module_eval 'def self.foo; :ok; end'; end; p M.foo"

assert ':ok', %{
$b = proc do p X end
module A
X = :ko
begin
module_eval &$b
rescue NameError
p :ok
end
end
}

assert ':ok', %{
module A
X = :ko
class B
def foo(&b)
(class << self; self; end).module_eval &b
end
end
end
begin
A::B.new.foo do p X end
rescue NameError
p :ok
end
}

assert '42', %{
class A; end
A.class_eval %{
def foo
baz = {}
bar = baz[:foo] = 42
bar
end
}
p A.new.foo
}

assert '42', 'a = nil; 1.times { a = 42; eval "p a" }'

assert 'main', "p eval('self')"
assert 'main', "p eval('self', binding)"
assert 'main', "p eval('self', proc{}.binding)"

assert '42', %{
b = nil
1.times {
a = 42
b = binding
}
eval "p a", b
}

assert '42', 'y = eval("proc {|x| p x}"); y.call(42)'

assert '42', 'eval("def test; a = 42; p a; end; test")'
assert '42', 'eval("a = 42; def test; a = 0; end; test; p a", TOPLEVEL_BINDING)'
assert '42', 'eval("a = 42; def test; a = 0; end; test; p a")'

assert 'true', %{
begin
eval("1 = 1")
rescue Exception => e
end
p e.backtrace.kind_of? Array
}

assert 'true', %{
class Foo
def get_binding
binding
end
end
b = Foo.new.get_binding { 42 }
p eval "block_given?", b
}

assert '42', %{
class Foo
def get_binding
binding
end
end
b = Foo.new.get_binding { 42 }
p eval "yield", b
}

assert '42', %{
class Foo
FOO=42
class_eval("def foo; FOO; end")
end
p Foo.new.foo
}
Something went wrong with that request. Please try again.