Skip to content

Commit 4d72f51

Browse files
committed
Remove not needed #eval calls
1 parent e06b749 commit 4d72f51

File tree

10 files changed

+74
-88
lines changed

10 files changed

+74
-88
lines changed

core/hash/hash_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646
a = 1
4747
b = 2
4848

49-
eval('{a:, b:}.should == { a: 1, b: 2 }')
49+
{a:, b:}.should == { a: 1, b: 2 }
5050
end
5151
end

core/method/parameters_spec.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,10 @@ def underscore_parameters(_, _, _ = 1, *_, _:, _: 2, **_, &_); end
259259

260260
it "adds block arg with name & for anonymous block argument" do
261261
object = Object.new
262+
def object.foo(&)
263+
end
262264

263-
eval(<<~RUBY).should == [[:block, :&]]
264-
def object.foo(&)
265-
end
266-
object.method(:foo).parameters
267-
RUBY
265+
object.method(:foo).parameters.should == [[:block, :&]]
268266
end
269267

270268
it "returns [:rest, :*], [:keyrest, :**], [:block, :&] for forward parameters operator" do

core/proc/parameters_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
end
6565

6666
it "regards keyword parameters in lambdas as required" do
67-
eval("lambda {|x:| }").parameters.first.first.should == :keyreq
67+
lambda {|x:| }.parameters.first.first.should == :keyreq
6868
end
6969

7070
it "sets the first element of each sub-Array to :rest for parameters prefixed with asterisks" do
@@ -131,7 +131,7 @@
131131
end
132132

133133
it "adds block arg with name & for anonymous block argument" do
134-
eval('-> & {}.parameters').should == [[:block, :&]]
134+
-> & {}.parameters.should == [[:block, :&]]
135135
end
136136

137137
it "does not add locals as block options with a block and splat" do

core/range/each_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@
4040

4141
it "works with endless ranges" do
4242
a = []
43-
eval("(-2..)").each { |x| break if x > 2; a << x }
43+
(-2..).each { |x| break if x > 2; a << x }
4444
a.should == [-2, -1, 0, 1, 2]
4545

4646
a = []
47-
eval("(-2...)").each { |x| break if x > 2; a << x }
47+
(-2...).each { |x| break if x > 2; a << x }
4848
a.should == [-2, -1, 0, 1, 2]
4949
end
5050

5151
it "works with String endless ranges" do
5252
a = []
53-
eval("('A'..)").each { |x| break if x > "D"; a << x }
53+
('A'..).each { |x| break if x > "D"; a << x }
5454
a.should == ["A", "B", "C", "D"]
5555

5656
a = []
57-
eval("('A'...)").each { |x| break if x > "D"; a << x }
57+
('A'...).each { |x| break if x > "D"; a << x }
5858
a.should == ["A", "B", "C", "D"]
5959
end
6060

core/range/step_spec.rb

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -406,108 +406,108 @@
406406
describe "with an endless range" do
407407
describe "and Integer values" do
408408
it "yield Integer values incremented by 1 when not passed a step" do
409-
eval("(-2..)").step { |x| break if x > 2; ScratchPad << x }
409+
(-2..).step { |x| break if x > 2; ScratchPad << x }
410410
ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
411411

412412
ScratchPad.record []
413-
eval("(-2...)").step { |x| break if x > 2; ScratchPad << x }
413+
(-2...).step { |x| break if x > 2; ScratchPad << x }
414414
ScratchPad.recorded.should eql([-2, -1, 0, 1, 2])
415415
end
416416

417417
it "yields Integer values incremented by an Integer step" do
418-
eval("(-5..)").step(2) { |x| break if x > 3; ScratchPad << x }
418+
(-5..).step(2) { |x| break if x > 3; ScratchPad << x }
419419
ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
420420

421421
ScratchPad.record []
422-
eval("(-5...)").step(2) { |x| break if x > 3; ScratchPad << x }
422+
(-5...).step(2) { |x| break if x > 3; ScratchPad << x }
423423
ScratchPad.recorded.should eql([-5, -3, -1, 1, 3])
424424
end
425425

426426
it "yields Float values incremented by a Float step" do
427-
eval("(-2..)").step(1.5) { |x| break if x > 1.0; ScratchPad << x }
427+
(-2..).step(1.5) { |x| break if x > 1.0; ScratchPad << x }
428428
ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
429429

430430
ScratchPad.record []
431-
eval("(-2..)").step(1.5) { |x| break if x > 1.0; ScratchPad << x }
431+
(-2..).step(1.5) { |x| break if x > 1.0; ScratchPad << x }
432432
ScratchPad.recorded.should eql([-2.0, -0.5, 1.0])
433433
end
434434
end
435435

436436
describe "and Float values" do
437437
it "yields Float values incremented by 1 and less than end when not passed a step" do
438-
eval("(-2.0..)").step { |x| break if x > 1.5; ScratchPad << x }
438+
(-2.0..).step { |x| break if x > 1.5; ScratchPad << x }
439439
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
440440

441441
ScratchPad.record []
442-
eval("(-2.0...)").step { |x| break if x > 1.5; ScratchPad << x }
442+
(-2.0...).step { |x| break if x > 1.5; ScratchPad << x }
443443
ScratchPad.recorded.should eql([-2.0, -1.0, 0.0, 1.0])
444444
end
445445

446446
it "yields Float values incremented by an Integer step" do
447-
eval("(-5.0..)").step(2) { |x| break if x > 3.5; ScratchPad << x }
447+
(-5.0..).step(2) { |x| break if x > 3.5; ScratchPad << x }
448448
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
449449

450450
ScratchPad.record []
451-
eval("(-5.0...)").step(2) { |x| break if x > 3.5; ScratchPad << x }
451+
(-5.0...).step(2) { |x| break if x > 3.5; ScratchPad << x }
452452
ScratchPad.recorded.should eql([-5.0, -3.0, -1.0, 1.0, 3.0])
453453
end
454454

455455
it "yields Float values incremented by a Float step" do
456-
eval("(-1.0..)").step(0.5) { |x| break if x > 0.6; ScratchPad << x }
456+
(-1.0..).step(0.5) { |x| break if x > 0.6; ScratchPad << x }
457457
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
458458

459459
ScratchPad.record []
460-
eval("(-1.0...)").step(0.5) { |x| break if x > 0.6; ScratchPad << x }
460+
(-1.0...).step(0.5) { |x| break if x > 0.6; ScratchPad << x }
461461
ScratchPad.recorded.should eql([-1.0, -0.5, 0.0, 0.5])
462462
end
463463

464464
it "handles infinite values at the start" do
465-
eval("(-Float::INFINITY..)").step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
465+
(-Float::INFINITY..).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
466466
ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
467467

468468
ScratchPad.record []
469-
eval("(-Float::INFINITY...)").step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
469+
(-Float::INFINITY...).step(2) { |x| ScratchPad << x; break if ScratchPad.recorded.size == 3 }
470470
ScratchPad.recorded.should eql([-Float::INFINITY, -Float::INFINITY, -Float::INFINITY])
471471
end
472472
end
473473

474474
describe "and String values" do
475475
it "yields String values incremented by #succ and less than or equal to end when not passed a step" do
476-
eval("('A'..)").step { |x| break if x > "D"; ScratchPad << x }
476+
('A'..).step { |x| break if x > "D"; ScratchPad << x }
477477
ScratchPad.recorded.should == ["A", "B", "C", "D"]
478478

479479
ScratchPad.record []
480-
eval("('A'...)").step { |x| break if x > "D"; ScratchPad << x }
480+
('A'...).step { |x| break if x > "D"; ScratchPad << x }
481481
ScratchPad.recorded.should == ["A", "B", "C", "D"]
482482
end
483483

484484
it "yields String values incremented by #succ called Integer step times" do
485-
eval("('A'..)").step(2) { |x| break if x > "F"; ScratchPad << x }
485+
('A'..).step(2) { |x| break if x > "F"; ScratchPad << x }
486486
ScratchPad.recorded.should == ["A", "C", "E"]
487487

488488
ScratchPad.record []
489-
eval("('A'...)").step(2) { |x| break if x > "F"; ScratchPad << x }
489+
('A'...).step(2) { |x| break if x > "F"; ScratchPad << x }
490490
ScratchPad.recorded.should == ["A", "C", "E"]
491491
end
492492

493493
it "raises a TypeError when passed a Float step" do
494-
-> { eval("('A'..)").step(2.0) { } }.should raise_error(TypeError)
495-
-> { eval("('A'...)").step(2.0) { } }.should raise_error(TypeError)
494+
-> { ('A'..).step(2.0) { } }.should raise_error(TypeError)
495+
-> { ('A'...).step(2.0) { } }.should raise_error(TypeError)
496496
end
497497

498498
ruby_version_is "3.4" do
499499
it "yields String values adjusted by step" do
500-
eval("('A'..)").step("A") { |x| break if x > "AAA"; ScratchPad << x }
500+
('A'..).step("A") { |x| break if x > "AAA"; ScratchPad << x }
501501
ScratchPad.recorded.should == ["A", "AA", "AAA"]
502502

503503
ScratchPad.record []
504-
eval("('A'...)").step("A") { |x| break if x > "AAA"; ScratchPad << x }
504+
('A'...).step("A") { |x| break if x > "AAA"; ScratchPad << x }
505505
ScratchPad.recorded.should == ["A", "AA", "AAA"]
506506
end
507507

508508
it "raises a TypeError when passed an incompatible type step" do
509-
-> { eval("('A'..)").step([]) { } }.should raise_error(TypeError)
510-
-> { eval("('A'...)").step([]) { } }.should raise_error(TypeError)
509+
-> { ('A'..).step([]) { } }.should raise_error(TypeError)
510+
-> { ('A'...).step([]) { } }.should raise_error(TypeError)
511511
end
512512
end
513513
end

language/block_spec.rb

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def obj.to_ary; raise "Exception raised in #to_ary" end
294294
end
295295

296296
it "may include a rescue clause" do
297-
eval("@y.z do raise ArgumentError; rescue ArgumentError; 7; end").should == 7
297+
@y.z do raise ArgumentError; rescue ArgumentError; 7; end.should == 7
298298
end
299299
end
300300

@@ -308,7 +308,7 @@ def obj.to_ary; raise "Exception raised in #to_ary" end
308308
end
309309

310310
it "may include a rescue clause" do
311-
eval('@y.z do || raise ArgumentError; rescue ArgumentError; 7; end').should == 7
311+
@y.z do || raise ArgumentError; rescue ArgumentError; 7; end.should == 7
312312
end
313313
end
314314

@@ -337,7 +337,7 @@ def obj.to_ary; raise "Exception raised in #to_ary" end
337337
end
338338

339339
it "may include a rescue clause" do
340-
eval('@y.s(1) do |x| raise ArgumentError; rescue ArgumentError; 7; end').should == 7
340+
@y.s(1) do |x| raise ArgumentError; rescue ArgumentError; 7; end.should == 7
341341
end
342342
end
343343

@@ -737,9 +737,9 @@ def obj.to_ary; raise "Exception raised in #to_ary" end
737737
end
738738

739739
it "accepts unnamed arguments" do
740-
eval("lambda { |_,_| }").should be_an_instance_of(Proc)
741-
eval("->(_,_) {}").should be_an_instance_of(Proc)
742-
eval("Proc.new { |_,_| }").should be_an_instance_of(Proc)
740+
lambda { |_,_| }.should be_an_instance_of(Proc)
741+
->(_,_) {}.should be_an_instance_of(Proc)
742+
Proc.new { |_,_| }.should be_an_instance_of(Proc)
743743
end
744744
end
745745

@@ -1002,10 +1002,8 @@ def a; 1; end
10021002
# tested more thoroughly in language/delegation_spec.rb
10031003
describe "Anonymous block forwarding" do
10041004
it "forwards blocks to other method that formally declares anonymous block" do
1005-
eval <<-EOF
1006-
def b(&); c(&) end
1007-
def c(&); yield :non_null end
1008-
EOF
1005+
def b(&); c(&) end
1006+
def c(&); yield :non_null end
10091007

10101008
b { |c| c }.should == :non_null
10111009
end
@@ -1015,37 +1013,29 @@ def c(&); yield :non_null end
10151013
end
10161014

10171015
it "works when it's the only declared parameter" do
1018-
eval <<-EOF
1019-
def inner; yield end
1020-
def block_only(&); inner(&) end
1021-
EOF
1016+
def inner; yield end
1017+
def block_only(&); inner(&) end
10221018

10231019
block_only { 1 }.should == 1
10241020
end
10251021

10261022
it "works alongside positional parameters" do
1027-
eval <<-EOF
1028-
def inner; yield end
1029-
def pos(arg1, &); inner(&) end
1030-
EOF
1023+
def inner; yield end
1024+
def pos(arg1, &); inner(&) end
10311025

10321026
pos(:a) { 1 }.should == 1
10331027
end
10341028

10351029
it "works alongside positional arguments and splatted keyword arguments" do
1036-
eval <<-EOF
1037-
def inner; yield end
1038-
def pos_kwrest(arg1, **kw, &); inner(&) end
1039-
EOF
1030+
def inner; yield end
1031+
def pos_kwrest(arg1, **kw, &); inner(&) end
10401032

10411033
pos_kwrest(:a, arg: 3) { 1 }.should == 1
10421034
end
10431035

10441036
it "works alongside positional arguments and disallowed keyword arguments" do
1045-
eval <<-EOF
1046-
def inner; yield end
1047-
def no_kw(arg1, **nil, &); inner(&) end
1048-
EOF
1037+
def inner; yield end
1038+
def no_kw(arg1, **nil, &); inner(&) end
10491039

10501040
no_kw(:a) { 1 }.should == 1
10511041
end

language/hash_spec.rb

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,33 @@
7575
end
7676

7777
it "recognizes '=' at the end of the key" do
78-
eval("{:a==>1}").should == {:"a=" => 1}
79-
eval("{:a= =>1}").should == {:"a=" => 1}
80-
eval("{:a= => 1}").should == {:"a=" => 1}
78+
{:a==>1}.should == {:"a=" => 1}
79+
{:a= =>1}.should == {:"a=" => 1}
80+
{:a= => 1}.should == {:"a=" => 1}
8181
end
8282

8383
it "with '==>' in the middle raises SyntaxError" do
8484
-> { eval("{:a ==> 1}") }.should raise_error(SyntaxError)
8585
end
8686

8787
it "recognizes '!' at the end of the key" do
88-
eval("{:a! =>1}").should == {:"a!" => 1}
89-
eval("{:a! => 1}").should == {:"a!" => 1}
88+
{:a! =>1}.should == {:"a!" => 1}
89+
{:a! => 1}.should == {:"a!" => 1}
9090

91-
eval("{a!:1}").should == {:"a!" => 1}
92-
eval("{a!: 1}").should == {:"a!" => 1}
91+
{a!:1}.should == {:"a!" => 1}
92+
{a!: 1}.should == {:"a!" => 1}
9393
end
9494

9595
it "raises a SyntaxError if there is no space between `!` and `=>`" do
9696
-> { eval("{:a!=> 1}") }.should raise_error(SyntaxError)
9797
end
9898

9999
it "recognizes '?' at the end of the key" do
100-
eval("{:a? =>1}").should == {:"a?" => 1}
101-
eval("{:a? => 1}").should == {:"a?" => 1}
100+
{:a? =>1}.should == {:"a?" => 1}
101+
{:a? => 1}.should == {:"a?" => 1}
102102

103-
eval("{a?:1}").should == {:"a?" => 1}
104-
eval("{a?: 1}").should == {:"a?" => 1}
103+
{a?:1}.should == {:"a?" => 1}
104+
{a?: 1}.should == {:"a?" => 1}
105105
end
106106

107107
it "raises a SyntaxError if there is no space between `?` and `=>`" do
@@ -127,7 +127,7 @@
127127

128128
it "accepts mixed 'key: value', 'key => value' and '\"key\"': value' syntax" do
129129
h = {:a => 1, :b => 2, "c" => 3, :d => 4}
130-
eval('{a: 1, :b => 2, "c" => 3, "d": 4}').should == h
130+
{a: 1, :b => 2, "c" => 3, "d": 4}.should == h
131131
end
132132

133133
it "expands an '**{}' element into the containing Hash literal initialization" do
@@ -260,22 +260,22 @@ def m(h)
260260
describe "hash with omitted value" do
261261
it "accepts short notation 'key' for 'key: value' syntax" do
262262
a, b, c = 1, 2, 3
263-
h = eval('{a:}')
263+
h = {a:}
264264
{a: 1}.should == h
265-
h = eval('{a:, b:, c:}')
265+
h = {a:, b:, c:}
266266
{a: 1, b: 2, c: 3}.should == h
267267
end
268268

269269
it "ignores hanging comma on short notation" do
270270
a, b, c = 1, 2, 3
271-
h = eval('{a:, b:, c:,}')
271+
h = {a:, b:, c:,}
272272
{a: 1, b: 2, c: 3}.should == h
273273
end
274274

275275
it "accepts mixed syntax" do
276276
a, e = 1, 5
277-
h = eval('{a:, b: 2, "c" => 3, :d => 4, e:}')
278-
eval('{a: 1, :b => 2, "c" => 3, "d": 4, e: 5}').should == h
277+
h = {a:, b: 2, "c" => 3, :d => 4, e:}
278+
{a: 1, :b => 2, "c" => 3, "d": 4, e: 5}.should == h
279279
end
280280

281281
it "works with methods and local vars" do

language/keyword_arguments_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def m(a:, b:)
332332
a = 1
333333
b = 2
334334

335-
eval('m(a:, b:).should == [1, 2]')
335+
m(a:, b:).should == [1, 2]
336336
end
337337
end
338338

0 commit comments

Comments
 (0)