@@ -1049,6 +1049,12 @@ def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **
10491049 } . should complain ( /warning: `it` calls without arguments will refer to the first block param in Ruby 3.4; use it\( \) or self.it/ )
10501050 end
10511051
1052+ it "emits a deprecation warning if numbered parameters are used" do
1053+ -> {
1054+ eval "proc { it; _1 }"
1055+ } . should complain ( /warning: `it` calls without arguments will refer to the first block param in Ruby 3.4; use it\( \) or self.it/ )
1056+ end
1057+
10521058 it "does not emit a deprecation warning when a block has parameters" do
10531059 -> { eval "proc { |a, b| it }" } . should_not complain
10541060 -> { eval "proc { |*rest| it }" } . should_not complain
@@ -1058,21 +1064,75 @@ def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **
10581064 -> { eval "proc { |**| it }" } . should_not complain
10591065 -> { eval "proc { |&block| it }" } . should_not complain
10601066 -> { eval "proc { |&| it }" } . should_not complain
1067+ -> { eval "proc { || it }" } . should_not complain
10611068 end
10621069
10631070 it "does not emit a deprecation warning when `it` calls with arguments" do
10641071 -> { eval "proc { it(42) }" } . should_not complain
1072+ -> { eval "proc { it 42 }" } . should_not complain
1073+ end
1074+
1075+ it "does not emit a deprecation warning when `it` calls with a block" do
1076+ -> { eval "proc { it {} }" } . should_not complain
1077+ end
1078+
1079+ it "does not emit a deprecation warning when a local variable inside the block named `it` exists" do
1080+ -> { eval "proc { it = 42; it }" } . should_not complain
10651081 end
10661082
10671083 it "does not emit a deprecation warning when `it` calls with explicit empty arguments list" do
10681084 -> { eval "proc { it() }" } . should_not complain
10691085 end
1086+
1087+ it "calls the method `it` if defined" do
1088+ o = Object . new
1089+ def o . it
1090+ 21
1091+ end
1092+ suppress_warning do
1093+ o . instance_eval ( "proc { it * 2 }" ) . call ( 1 ) . should == 42
1094+ end
1095+ end
1096+ end
1097+
1098+ ruby_version_is "3.4" do
1099+ it "does not emit a deprecation warning" do
1100+ -> {
1101+ eval "proc { it }"
1102+ } . should_not complain
1103+ end
1104+
1105+ it "acts as the first argument if no local variables exist" do
1106+ eval ( "proc { it * 2 }" ) . call ( 5 ) . should == 10
1107+ end
1108+
1109+ it "can be reassigned to act as a local variable" do
1110+ eval ( "proc { tmp = it; it = tmp * 2; it }" ) . call ( 21 ) . should == 42
1111+ end
1112+
1113+ it "can be used in nested calls" do
1114+ eval ( "proc { it.map { it * 2 } }" ) . call ( [ 1 , 2 , 3 ] ) . should == [ 2 , 4 , 6 ]
1115+ end
1116+
1117+ it "cannot be mixed with numbered parameters" do
1118+ -> {
1119+ eval "proc { it + _1 }"
1120+ } . should raise_error ( SyntaxError , /numbered parameters are not allowed when 'it' is already used|'it' is already used in/ )
1121+
1122+ -> {
1123+ eval "proc { _1 + it }"
1124+ } . should raise_error ( SyntaxError , /numbered parameter is already used in|`it` is not allowed when a numbered parameter is already used/ )
1125+ end
10701126 end
10711127end
10721128
1073- describe "if `it` is defined outside of a block " do
1074- it "treats `it` as a captured variable" do
1129+ describe "if `it` is defined as a variable " do
1130+ it "treats `it` as a captured variable if defined outside of a block " do
10751131 it = 5
10761132 proc { it } . call ( 0 ) . should == 5
10771133 end
1134+
1135+ it "treats `it` as a local variable if defined inside of a block" do
1136+ proc { it = 5 ; it } . call ( 0 ) . should == 5
1137+ end
10781138end
0 commit comments