Passing a block to Rack::Response#finish

Language: Ruby

# Passing a block to Rack::Response#finish
#
# You can pass a block to #write, which yields self;
# calls to #write inside the block will write directly to the
# output stream instead of Rack::Response's internal buffer
class App
  def call(env)
    Rack::Response.new.finish do |resp|
      resp.write "one\n"
      sleep 1
      resp.write "two\n"
      sleep 1
      resp.write "three\n"
    end
  end
end

run App.new

# Run with "rackup" command; use curl to connect:
#
#   > curl localhost:9292
#   one
#   two
#   three
Reveal More
Added about 2 years ago by Devo_normal gbuesing