Skip to content

Commit 22cf6b0

Browse files
committed
Fix parameters forwarding in eval
1 parent 14ae975 commit 22cf6b0

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

core/kernel/eval_spec.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,75 @@ class Object
175175
end
176176
end
177177

178+
context "parameter forwarding" do
179+
it "allows anonymous rest parameter forwarding" do
180+
object = Object.new
181+
def object.foo(a, b, c)
182+
[a, b, c]
183+
end
184+
def object.bar(*)
185+
eval "foo(*)"
186+
end
187+
188+
object.bar(1, 2, 3).should == [1, 2, 3]
189+
end
190+
191+
it "allows anonymous keyword parameters forwarding" do
192+
object = Object.new
193+
def object.foo(a:, b:, c:)
194+
[a, b, c]
195+
end
196+
def object.bar(**)
197+
eval "foo(**)"
198+
end
199+
200+
object.bar(a: 1, b: 2, c: 3).should == [1, 2, 3]
201+
end
202+
203+
it "allows anonymous block parameter forwarding" do
204+
object = Object.new
205+
def object.foo(&block)
206+
block.call
207+
end
208+
def object.bar(&)
209+
eval "foo(&)"
210+
end
211+
212+
object.bar { :foobar }.should == :foobar
213+
end
214+
215+
it "allows ... forwarding" do
216+
object = Object.new
217+
def object.foo(a, b:, &block)
218+
[a, b, block.call]
219+
end
220+
def object.bar(...)
221+
eval "foo(...)"
222+
end
223+
224+
object.bar(1, b: 2) { 3 }.should == [1, 2, 3]
225+
end
226+
227+
it "allows parameter forwarding to super" do
228+
m = Module.new do
229+
def foo(a, b:, &block)
230+
[a, b, block.call]
231+
end
232+
end
233+
234+
c = Class.new do
235+
include m
236+
237+
def foo(a, b:, &block)
238+
eval "super"
239+
end
240+
end
241+
242+
object = c.new
243+
object.foo(1, b: 2) { 3 }.should == [1, 2, 3]
244+
end
245+
end
246+
178247
ruby_version_is "3.3" do
179248
it "uses (eval at __FILE__:__LINE__) if none is provided" do
180249
eval("__FILE__").should == "(eval at #{__FILE__}:#{__LINE__})"

0 commit comments

Comments
 (0)