File tree 2 files changed +39
-5
lines changed
2 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -66,11 +66,15 @@ def finish
66
66
67
67
# Enumerate all chunks until finished, then invoke `#close`.
68
68
def each
69
- while chunk = self . read
70
- yield chunk
69
+ return to_enum ( :each ) unless block_given?
70
+
71
+ begin
72
+ while chunk = self . read
73
+ yield chunk
74
+ end
75
+ ensure
76
+ self . close ( $!)
71
77
end
72
- ensure
73
- self . close ( $!)
74
78
end
75
79
76
80
# Read all remaining chunks into a single binary string using `#each`.
Original file line number Diff line number Diff line change 122
122
expect ( body . read ) . to be == "Hello"
123
123
end
124
124
end
125
-
125
+
126
+ with "#each" do
127
+ with "a block" do
128
+ it "iterates over chunks" do
129
+ result = [ ]
130
+ body . each { |chunk | result << chunk }
131
+ expect ( result ) . to be == source
132
+ end
133
+ end
134
+
135
+ with "no block" do
136
+ it "returns an enumerator" do
137
+ expect ( body . each ) . to be_a ( Enumerator )
138
+ end
139
+
140
+ it "can be chained with enumerator methods" do
141
+ result = [ ]
142
+
143
+ body . each . with_index do |chunk , index |
144
+ if index . zero?
145
+ result << chunk . upcase
146
+ else
147
+ result << chunk . downcase
148
+ end
149
+ end
150
+
151
+ expect ( result ) . to be == [ "HELLO" , "world" ]
152
+ end
153
+ end
154
+ end
155
+
126
156
with '#inspect' do
127
157
it "can be inspected" do
128
158
expect ( body . inspect ) . to be =~ /\d + chunks, \d + bytes/
You can’t perform that action at this time.
0 commit comments