I'm pretty curious about how this thing works.
after require 'sinatra'
then I can invoke get() in the top level scope.
after digging into the source code, I found this get() structure
module Sinatra
class << self
def get
...
end
end
end
know the class << self is open up the self object's singleton class definition and add get() inside, so it starts to make sense.
But the only thing left I can't figure out is it's within module Sinstra, how could get() be invoked without using Sinatra:: resolution operation or something?
I haven't looked at the source of Sinatra, but the gist of it should be something like
It is spread out in a few places, but if you look in
lib/sinatra/main.rb
, you can see this line at the bottom:include Sinatra::Delegator
If we go into
lib/sinatra/base.rb
we see this chunk of code around like 1470.This code does what the comment says: if it is included, it delegates all calls to the list of delegated methods to
Sinatra::Application
class, which is a subclass ofSinatra::Base
, which is where theget
method is defined. When you write something like this:Sinatra will end up calling the
get
method onSinatra::Base
due to the delegation it set up earlier.