24

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?

ez.
  • 7,604
  • 5
  • 30
  • 29
  • I would assume so that it can be buffered up and then sent in chunks to whoever requested it. The response can be quite large so the servers "stream" it out. is your question more specific to the clause about Ruby 1.9 other than the whole iteration over the body? – Pavel Veller May 08 '12 at 03:46
  • It also makes sense when you think about how send() and recv() work in the C world. When you call recv you give it an address to a buffer of characters that it fills. You're not sure to get the entire body in one call. The same is true when you send a series of characters. – nickethier May 08 '12 at 03:58

2 Answers2

11

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

user570783
  • 686
  • 4
  • 7
  • 3
    right. and when your component that you stick in there does something more than sending a pre-built array of strings, Rack gives you a nice `enumerable` contract to report your progress in chunks in response to `each`. While it may look silly for a hello world example, it makes much more sense with something more real, like a template engine spitting our generated HTML, for example – Pavel Veller May 08 '12 at 03:53
  • 2
    Link-only answers are discouraged because the site you are linking to could go down or change. Please write the important points here. – David Grayson Aug 17 '16 at 19:52
7

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.

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158