@@ -1049,6 +1049,12 @@ def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **
1049
1049
} . should complain ( /warning: `it` calls without arguments will refer to the first block param in Ruby 3.4; use it\( \) or self.it/ )
1050
1050
end
1051
1051
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
+
1052
1058
it "does not emit a deprecation warning when a block has parameters" do
1053
1059
-> { eval "proc { |a, b| it }" } . should_not complain
1054
1060
-> { eval "proc { |*rest| it }" } . should_not complain
@@ -1058,21 +1064,71 @@ def all_kwrest(arg1, arg2, *rest, post1, post2, kw1: 1, kw2: 2, okw1:, okw2:, **
1058
1064
-> { eval "proc { |**| it }" } . should_not complain
1059
1065
-> { eval "proc { |&block| it }" } . should_not complain
1060
1066
-> { eval "proc { |&| it }" } . should_not complain
1067
+ -> { eval "proc { || it }" } . should_not complain
1061
1068
end
1062
1069
1063
1070
it "does not emit a deprecation warning when `it` calls with arguments" do
1064
1071
-> { 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
1065
1081
end
1066
1082
1067
1083
it "does not emit a deprecation warning when `it` calls with explicit empty arguments list" do
1068
1084
-> { eval "proc { it() }" } . should_not complain
1069
1085
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/ )
1121
+ end
1070
1122
end
1071
1123
end
1072
1124
1073
- describe "if `it` is defined outside of a block " do
1074
- it "treats `it` as a captured variable" do
1125
+ describe "if `it` is defined as a variable " do
1126
+ it "treats `it` as a captured variable if defined outside of a block " do
1075
1127
it = 5
1076
1128
proc { it } . call ( 0 ) . should == 5
1077
1129
end
1130
+
1131
+ it "treats `it` as a local variable if defined inside of a block" do
1132
+ proc { it = 5 ; it } . call ( 0 ) . should == 5
1133
+ end
1078
1134
end
0 commit comments