它分散在几个地方,但如果你查看lib/sinatra/main.rb,你可以在底部看到这一行:
include Sinatra::Delegator
如果我们进入lib/sinatra/base.rb,我们会看到这段代码大约是 1470。
# Sinatra delegation mixin. Mixing this module into an object causes all
# methods to be delegated to the Sinatra::Application class. Used primarily
# at the top-level.
module Delegator #:nodoc:
def self.delegate(*methods)
methods.each do |method_name|
define_method(method_name) do |*args, &block|
return super(*args, &block) if respond_to? method_name
Delegator.target.send(method_name, *args, &block)
end
private method_name
end
end
delegate :get, :patch, :put, :post, :delete, :head, :options, :template, :layout,
:before, :after, :error, :not_found, :configure, :set, :mime_type,
:enable, :disable, :use, :development?, :test?, :production?,
:helpers, :settings
class << self
attr_accessor :target
end
self.target = Application
end
这段代码执行注释所说的:如果包含它,它将所有对委托方法列表的调用委托给Sinatra::Application类,它是Sinatra::Base的子类,这是get方法所在的地方定义。当你写这样的东西时:
require "sinatra"
get "foo" do
"Hello World"
end
由于之前设置的委托,Sinatra 最终将在 Sinatra::Base 上调用 get 方法。