在您给出的示例中,ExtensionBar 不会对任何应用程序执行任何操作。您还需要在您希望在应用程序中使用的扩展中注册依赖扩展。编写模块的说明给出了一个before
块作为 LinkBlocker DSL 的示例,这两者都会使您的示例更像这样:
# extension_foo.rb
require 'sinatra/base'
module Sinatra
module ExtensionFoo
def with_foo
warn "Calling with_foo"
s = yield
warn "s = #{s}"
s
end
end
register ExtensionFoo
end
# extension_bar.rb
require 'sinatra/base'
require_relative 'extension_foo.rb'
module Sinatra
module ExtensionBar
before do
warn "Calling with_foo in before"
with_foo do
"bar"
end
end
end
register ExtensionBar
end
# app.rb
require 'sinatra'
require_relative 'extension_bar.rb'
get "/" do
with_foo do
"blah"
end.inspect
end
当我运行它时,我没有收到错误,并且确实在我的 STDOUT 中看到了警告,并且输出为“blah”。
Calling with_foo in before
Calling with_foo
from /Volumes/RubyProjects/Test/extension_dependency/extension_foo.rb:6:in `with_foo'
s = bar
from /Volumes/RubyProjects/Test/extension_dependency/extension_foo.rb:8:in `with_foo'
Calling with_foo
from /Volumes/RubyProjects/Test/extension_dependency/extension_foo.rb:6:in `with_foo'
s = blah
from /Volumes/RubyProjects/Test/extension_dependency/extension_foo.rb:8:in `with_foo'