在模块化 Sinatra 应用程序中访问 irb

问题描述:

我正在构建一个像这样子类化 Sinatra 的应用程序:

I am building an application which subclasses Sinatra like so:

require 'rubygems'
require 'sinatra/base'
require 'sinatra/assetpack'

class App < Sinatra::Base
  ...

  run!
end

我如何访问 irb?以这种方式执行 sinatra 时不会解析选项,如何以编程方式打开 irb shell?

How can I access irb? Options are not parsed when executing sinatra this way, how do I programmatically open an irb shell?

我有点困惑是要从应用程序中打开 IRB 会话 (?) 还是使用 IRB 来调试 Sinatra 项目?

I'm a little confused whether you want to open an IRB session from within your app (?) or use IRB to debug your Sinatra project?

为了调试基于 Rack 的应用程序(例如 Sinatra),我喜欢使用 racksh gem,对于 Rack 应用程序来说,它就像 Rails 中的脚本/控制台".它相对于 IRB 的主要优势在于,racksh 将整个应用程序环境加载到 shell 中,使调试变得轻而易举.

For debugging Rack-based apps (such as Sinatra), I like using the racksh gem, which "is like script/console in Rails" for Rack applications. Its main advantage over IRB is that racksh loads the entire application environment into the shell, making debugging a breeze.

来自racksh 的Github 页面:它的目的是让开发人员能够自省他的应用程序和/或进行一些初始设置.例如,您可以运行 DataMapper.auto_migrate! 或向/users/666 发出请求并检查响应详细信息.它主要针对没有类似控制台组件的应用程序(即使用 Sinatra 构建的应用程序)) 但所有框架都可以从交互式 Rack 堆栈和请求自省中受益."

From racksh's Github page: "It's purpose is to allow developer to introspect his application and/or make some initial setup. You can for example run DataMapper.auto_migrate! or make a request to /users/666 and check response details. It's mainly aimed at apps that don't have console-like component (ie. apps built with Sinatra) but all frameworks can benefit from interactive Rack stack and request introspection."

然而,racksh 要求您的应用程序有一个 config.ru 文件,因此您必须重新编写您的应用程序:

However, racksh requires your app to have a config.ru file, so you would have to re-write your app:

# in config.ru
require 'rubygems'
require 'sinatra/base'
require 'sinatra/assetpack'
require 'app.rb'


# in app.rb
class App < Sinatra::Base
  ...

  run!
end

然后在您的应用程序文件夹(config.ru 所在的位置)中:

Then in your app folder (where config.ru resides):

$ gem install racksh # or add gem 'racksh' to your Gemfile and run bundle
$ racksh