问题
a classic hello world example from their doc,
class HelloWorld
def call(env)
return [200, {}, ["Hello world!"]]
end
end
my question is why the third value is [Hello world!"], not "Hello world"? From their doc,
The Body must respond to each and must only yield String values. The Body itself should not be an instance of String, as this will break in Ruby 1.9.
Why body needs to respond to each? and in what case does it matter?
回答1:
I think rack originated on python's wsgi. Here is the explanation for python: http://www.python.org/dev/peps/pep-3333/#buffering-and-streaming
回答2:
Judging from the sentence The Body itself should not be an instance of String, as this will break in Ruby 1.9.
, I'd assume that the last element of the response has to be an Enumerable
. Since String
s stopped including this module in 1.9, you'll need some sort of collection that implements it.
BTW: hello world examples are often not great for figuring out why something is done in a certain way, they are too trivial.
来源:https://stackoverflow.com/questions/10492181/why-is-rack-response-body-an-array-not-a-string