summaryrefslogtreecommitdiff
path: root/spec/ruby/core/kernel/eval_spec.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2023-07-27 08:26:59 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2023-07-30 00:44:13 +0900
commit3d87eec94a4778cabd48726b00867a0092de5ad0 (patch)
tree5f0e22e8cf4f60c60620e95c4fe651cd88656050 /spec/ruby/core/kernel/eval_spec.rb
parentdf5330b04eda80d00a1f406573ae6b9e5f71c533 (diff)
Add examples of `return` in `eval`
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/8127
Diffstat (limited to 'spec/ruby/core/kernel/eval_spec.rb')
-rw-r--r--spec/ruby/core/kernel/eval_spec.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/spec/ruby/core/kernel/eval_spec.rb b/spec/ruby/core/kernel/eval_spec.rb
index 4acb87c729..3dfc863368 100644
--- a/spec/ruby/core/kernel/eval_spec.rb
+++ b/spec/ruby/core/kernel/eval_spec.rb
@@ -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"