我在模块化 Sinatra 应用程序中使用 Ruby URI模块的方法时遇到问题。encode_www_form由于某种原因,URI被解释为URI::Parser子类,因此方法调用失败是可以理解的。
我已将其简化为最小的测试用例。Gemfile: _
source 'https://rubygems.org'
ruby '1.9.3'
gem 'sinatra'
并且app.rb:
require 'sinatra/base'
class Frontend < Sinatra::Base
get '/test/' do
URI.encode_www_form(:a => 1, :b => 2)
end
run! if app_file == $0
end
如果我然后运行ruby app.rb并访问,/test/我会得到:
NoMethodError - undefined method `encode_www_form' for #<URI::Parser:0x007fa9221ca868>:
app.rb:6:in `block in <class:Frontend>'
如果我将其转换为经典风格的 Sinatra 应用程序,app.rb则如下所示:
require 'sinatra'
get '/test/' do
URI.encode_www_form(:a => 1, :b => 2)
end
然后调用ruby app.rb访问/test/,页面根据需要显示“a=1&b=2”。
那么,意味着 URI 出现问题的模块化格式出了什么问题?