Skip to content

Commit 72959eb

Browse files
committed
Remove to_ary from Response
Responses are not arrays, so we should not allow them to be implicitly coerced to an array. If you would like you response to be converted to an array, you must explicitly do it with the `to_a` method. This also prevents creation of unnecessary body proxies
1 parent 7fecaee commit 72959eb

4 files changed

Lines changed: 5 additions & 11 deletions

File tree

lib/rack/body_proxy.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ def initialize(body, &block)
99
end
1010

1111
def respond_to?(method_name, include_all = false)
12-
case method_name
13-
when :to_ary, 'to_ary'
14-
return false
15-
end
1612
super or @body.respond_to?(method_name, include_all)
1713
end
1814

@@ -39,7 +35,6 @@ def each
3935
end
4036

4137
def method_missing(method_name, *args, &block)
42-
super if :to_ary == method_name
4338
@body.__send__(method_name, *args, &block)
4439
end
4540
end

lib/rack/response.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,10 @@ def finish(&block)
7272
close
7373
[status.to_i, header, []]
7474
else
75-
[status.to_i, header, BodyProxy.new(self){}]
75+
[status.to_i, header, self]
7676
end
7777
end
7878
alias to_a finish # For *response
79-
alias to_ary finish # For implicit-splat on Ruby 1.9.2
8079

8180
def each(&callback)
8281
@body.each(&callback)

test/spec_body_proxy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def object.close() raise "No!" end
6363
body.respond_to?(:to_ary).must_equal true
6464

6565
proxy = Rack::BodyProxy.new(body) { }
66-
proxy.respond_to?(:to_ary).must_equal false
67-
proxy.respond_to?("to_ary").must_equal false
66+
x = [proxy]
67+
assert_equal x, x.flatten
6868
end
6969

7070
it 'not close more than one time' do

test/spec_response.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ def object_with_each.each
466466

467467
it "wraps the body from #to_ary to prevent infinite loops" do
468468
res = Rack::Response.new
469-
res.finish.last.wont_respond_to(:to_ary)
470-
lambda { res.finish.last.to_ary }.must_raise NoMethodError
469+
x = res.finish
470+
assert_equal x, x.flatten
471471
end
472472
end
473473

0 commit comments

Comments
 (0)