File tree Expand file tree Collapse file tree 2 files changed +39
-5
lines changed Expand file tree Collapse file tree 2 files changed +39
-5
lines changed Original file line number Diff line number Diff line change @@ -66,11 +66,15 @@ def finish
6666
6767 # Enumerate all chunks until finished, then invoke `#close`.
6868 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 ( $!)
7177 end
72- ensure
73- self . close ( $!)
7478 end
7579
7680 # Read all remaining chunks into a single binary string using `#each`.
Original file line number Diff line number Diff line change 122122 expect ( body . read ) . to be == "Hello"
123123 end
124124 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+
126156 with '#inspect' do
127157 it "can be inspected" do
128158 expect ( body . inspect ) . to be =~ /\d + chunks, \d + bytes/
You can’t perform that action at this time.
0 commit comments