Add examples of `return` in `eval`

This commit is contained in:
Nobuyoshi Nakada 2023-07-27 08:26:59 +09:00
Родитель df5330b04e
Коммит 3d87eec94a
1 изменённых файлов: 14 добавлений и 0 удалений

Просмотреть файл

@ -226,6 +226,20 @@ describe "Kernel#eval" do
-> { eval("return :eval") }.call.should == :eval
end
it "returns from the method calling #eval when evaluating 'return'" do
def eval_return(n)
eval("return n*2")
end
-> { eval_return(3) }.call.should == 6
end
it "returns from the method calling #eval when evaluating 'return' in BEGIN" do
def eval_return(n)
eval("BEGIN {return n*3}")
end
-> { eval_return(4) }.call.should == 12
end
it "unwinds through a Proc-style closure and returns from a lambda-style closure in the closure chain" do
code = fixture __FILE__, "eval_return_with_lambda.rb"
ruby_exe(code).chomp.should == "a,b,c,eval,f"