diff options
author | Samuel Williams <samuel.williams@oriontransfer.co.nz> | 2022-08-05 02:36:13 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-04 07:36:13 -0700 |
commit | 1e4c18d895cde0af92e9286cfda52b4c10bb0488 (patch) | |
tree | c31e9a8c2781a0579df10a92a2b7198d7ab294a5 /lib/rack/builder.rb | |
parent | 882cbb32d6472bbaa386ca220d2512ccfdcb86c0 (diff) | |
download | rack-1e4c18d895cde0af92e9286cfda52b4c10bb0488.tar.gz |
Allow `Rack::Builder#run` to take a block instead of an argument. (#1942)
* Raise ArgumentError if both app and block given.
Diffstat (limited to 'lib/rack/builder.rb')
-rw-r--r-- | lib/rack/builder.rb | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/lib/rack/builder.rb b/lib/rack/builder.rb index fa5e876f..6d40b534 100644 --- a/lib/rack/builder.rb +++ b/lib/rack/builder.rb @@ -157,22 +157,42 @@ module Rack ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true) # :nocov: - # Takes an argument that is an object that responds to #call and returns a Rack response. - # The simplest form of this is a lambda object: + # Takes a block or argument that is an object that responds to #call and + # returns a Rack response. + # + # You can use a block: + # + # run do |env| + # [200, { "content-type" => "text/plain" }, ["Hello World!"]] + # end + # + # You can also provide a lambda: # # run lambda { |env| [200, { "content-type" => "text/plain" }, ["OK"]] } # - # However this could also be a class: + # You can also provide a class instance: # # class Heartbeat - # def self.call(env) + # def call(env) # [200, { "content-type" => "text/plain" }, ["OK"]] # end # end # - # run Heartbeat - def run(app) - @run = app + # run Heartbeat.new + # + # It could also be a module: + # + # module HelloWorld + # def call(env) + # [200, { "content-type" => "text/plain" }, ["Hello World"]] + # end + # end + # + # run HelloWorld + def run(app = nil, &block) + raise ArgumentError, "Both app and block given!" if app && block_given? + + @run = app || block end # Takes a lambda or block that is used to warm-up the application. This block is called |