简体   繁体   English

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

[英]Accessing the irb in a modular Sinatra application

I am building an application which subclasses Sinatra like so: 我正在构建一个将Sinatra子类化的应用程序,如下所示:

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

class App < Sinatra::Base
  ...

  run!
end

How can I access irb? 如何访问irb? Options are not parsed when executing sinatra this way, how do I programmatically open an irb shell? 以这种方式执行sinatra时,不会解析选项,如何以编程方式打开irb shell?

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? 您是要在应用程序中打开IRB会话(?),还是要使用IRB调试Sinatra项目,我有些困惑。

For debugging Rack-based apps (such as Sinatra), I like using the racksh gem , which " is like script/console in Rails " for Rack applications. 对于调试基于Rack的应用程序(例如Sinatra),我喜欢使用racksh gem ,它对于Rack应用程序来说就像Rails中的脚本/控制台 ”。 Its main advantage over IRB is that racksh loads the entire application environment into the shell, making debugging a breeze. 与IRB相比,它的主要优势在于,sharksh将整个应用程序环境加载到shell中,从而使调试变得轻而易举。

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的Github页面上 :“其目的是允许开发人员自检其应用程序和/或进行一些初始设置。例如,您可以运行DataMapper.auto_migrate!或向/ users / 666发出请求并检查响应详细信息。它的主要目的是不具有类似控制台的组件的应用程序(例如,使用Sinatra构建的应用程序),但是所有框架都可以从交互式Rack堆栈中受益并请求自省。”

However, racksh requires your app to have a config.ru file, so you would have to re-write your app: 但是,racksh要求您的应用程序具有config.ru文件,因此您必须重新编写应用程序:

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


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

  run!
end

Then in your app folder (where config.ru resides): 然后在您的应用程序文件夹(config.ru所在的文件夹)中:

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

只需键入以下内容(在shell提示符下):

irb -r ./my_app.rb

Check this simple search interface for Microsoft's Bing using Sinatra and binger gem. 使用Sinatrabinger gem,在此简单的搜索界面中查看Microsoft的Bing。 If you follow the instructions from there you will understand better. 如果按照那里的说明进行操作,将会更好地理解。

First at all, create a Gemfile and add: 首先,创建一个Gemfile并添加:

source "https://rubygems.org"

gem 'sinatra'
gem 'binger'

Then run the bundle command that will generated Gemfile.lock . 然后运行将生成Gemfile.lockbundle命令。 Then create a config.ru file, and add by example: 然后创建一个config.ru文件,并通过示例添加:

require 'rubygems'
require 'bundler'

Bundler.require

require './app.rb'

run MyApp

Your app.rb could look like this: 您的app.rb可能如下所示:

class MyApp < Sinatra::Base

  get '/' do

      @title = "Index" 

      erb:index

  end
end

You must have a folder named views . 您必须有一个名为views的文件夹。 Create index.erb and add: 创建index.erb并添加:

< % = @title % >

Finally, run rackup . 最后,运行rackup

Source: https://github.com/thinkphp/sinatra-bing 资料来源: https : //github.com/thinkphp/sinatra-bing

Demo: http://sinatra-bing.herokuapp.com/ 演示: http//sinatra-bing.herokuapp.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM