How Sinatra avoids polluting inheritance chain
When you open a new file:
[13] pry(main)> self
=> main
[14] pry(main)> self.class
=> Object
main is an instance of Object
An example: the Sinatra DSL
require 'sinatra'
get '/' do
'Hello world!'
end
#get is put in main's singleton class
[1] pry(main)> require 'sinatra'
=> true
[2] pry(main)> method(:get).source_location
=> .../sinatra-1.4.6/lib/sinatra/base.rb", 1987]
pry(main)> Object.new.get
NoMethodError: undefined method `get' for #<Object:0x007fc423d9ec18>
This is achieved by extending the singleton class of main.
from Sinatra:
extend Sinatra::Delegator
A simple example:
class Foo
module DSL
def foo
'bar'
end
end
end
extend ::Foo::DSL
Tweet