Why is rack response body an array not a string?

浪子不回头ぞ 提交于 2019-12-21 06:57:45

问题


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 Strings 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!