Proc throws error when used with do end [duplicate]

▼魔方 西西 提交于 2019-12-02 06:52:59

问题


This doesn't work:

run Proc.new do |env|
  [200, 
   {
    "Content-Type" => "application/json; charset=UTF-8"
  }, ["{\"name\":\"Rack App\"}"]]
end

But this does:

run Proc.new { |env|
  [200, 
   {
    "Content-Type" => "application/json; charset=UTF-8"
  }, ["{\"name\":\"Rack App\"}"]]
}

Any ideas, why its throwing error when used with do..end?

Error I am getting:

app.ru:1:in new: tried to create Proc object without a block (ArgumentError)


回答1:


Your first code is interpreted as:

run(Proc.new) do |env|
  ...
end

and the block is is passed to run instead of new. The problem can be solved by doing:

run(Proc.new do |env|
  ...
end)


来源:https://stackoverflow.com/questions/25217274/proc-throws-error-when-used-with-do-end

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!