西纳特拉 +捆绑器?

发布于 2024-08-10 20:49:36 字数 74 浏览 2 评论 0原文

我想知道如何将 Bundler 与 Sinatra 一起使用。这个想法是使用 Bundler 在 .gems 文件夹中下载的 gem。

I'm wondering how one can use Bundler with Sinatra. The idea is to use the gems that Bundler downloads inside the .gems folder.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

锦爱 2024-08-17 20:49:36

在您的 Sinatra 应用程序中,您只需要求捆绑程序设置:

require "bundler/setup"
require "sinatra"

get "/" do
  "Hello world!"
end

或者,如果您不想在应用程序顶部添加额外的 require "bundler/setup",您可以改为调用sinatra via bundle exec (例如 bundle exec ruby​​ myapp.rb

这假设您的应用程序根目录中有一个 Gemfile。它可能看起来像这样:

source "http://rubygems.org"

gem "sinatra"

这还假设您已经安装了捆绑器(gem install bundler)并且运行了bundle install来安装所有gem依赖项。

Inside your Sinatra app, you just have to require the bundler setup:

require "bundler/setup"
require "sinatra"

get "/" do
  "Hello world!"
end

Alternatively, if you don't want to add the additional require "bundler/setup" at the top of your app, you can instead invoke sinatra via bundle exec (e.g. bundle exec ruby myapp.rb)

This assumes that you have a Gemfile in the root of your application. It might look like this:

source "http://rubygems.org"

gem "sinatra"

This also assumes that you've already installed bundler (gem install bundler) and that you ran bundle install to install all the gem dependencies.

等往事风中吹 2024-08-17 20:49:36

我相信最好的方法这里描述< /a> 在 EngineYard 博客上:

# This makes sure the bundled gems are in our $LOAD_PATH
require File.expand_path(File.join(File.dirname(__FILE__), 'vendor', 'gems', 'environment'))

# This actually requires the bundled gems
Bundler.require_env

class MyApp < Sinatra::Base
  # stuff
end

I believe the best way is described here on EngineYard blog:

# This makes sure the bundled gems are in our $LOAD_PATH
require File.expand_path(File.join(File.dirname(__FILE__), 'vendor', 'gems', 'environment'))

# This actually requires the bundled gems
Bundler.require_env

class MyApp < Sinatra::Base
  # stuff
end
花伊自在美 2024-08-17 20:49:36

由于我原来的答案很旧,但似乎仍然关注这个主题,这里是最新版本的bundler/sinatra设置,它将涵盖大部分用例:

一个最小的config.ru

require './my_sinatra_app'
run MySinatraApp

一个环境< code>env.rb 文件,需要所有捆绑的 gem(还支持加载当前环境的组):

require 'bundler/setup'

APP_ENV = ENV["RACK_ENV"] || "development"

Bundler.require :default, APP_ENV.to_sym

然后是您的应用程序文件(需要环境)和您的 sinatra 应用程序 (Sinatra::Base):

require_relative 'env'

class MyApp < Sinatra::Base
  get "/" do
    "hello world"
  end
end

使用rackup启动你的开发服务器,Sinatra将通过Bundler加载,你的应用程序可以从http://localhost访问: 9292。

$ rackup

bundle execrackup(如果需要)

确保您有一个如下所示的 Gemfile,并且在启动应用程序之前运行 bundle 命令

source "https://rubygems.org"

gem "sinatra"

gem "puma" # a better rack server than the default webrick

As my original answer was quite old but there seems to be still attention to this topic here's the latest version of bundler/sinatra setup which will cover most of the use case:

A minimal config.ru

require './my_sinatra_app'
run MySinatraApp

An environment env.rb file that requires all the bundled gems (also supports loading the current environment's group):

require 'bundler/setup'

APP_ENV = ENV["RACK_ENV"] || "development"

Bundler.require :default, APP_ENV.to_sym

Then your app file (requiring the environment) with your sinatra app (Sinatra::Base):

require_relative 'env'

class MyApp < Sinatra::Base
  get "/" do
    "hello world"
  end
end

Start your development server with rackup, and Sinatra will be loaded via Bundler, your app will be accessible from http://localhost:9292.

$ rackup

or bundle exec rackup if needed

Make sure you have a Gemfile like the following one and you run the bundle command before starting the app

source "https://rubygems.org"

gem "sinatra"

gem "puma" # a better rack server than the default webrick
身边 2024-08-17 20:49:36

+1 捆绑器网站上的指南,但如果您有一个简单的应用程序并在顶层,那么您需要执行以下操作:

在您的 Gemfile 中(告诉捆绑程序不需要 sinatra):

gem 'sinatra', :require => false

以及在应用程序的文件中(明确要求 sinatra):

require 'rubygems'
require 'bundler'
Bundler.require
require 'sinatra'

get '/' do
  'hello world'
end

+1 for the guide on the bundler website, but if you have a simple app and use Sinatra's dsl at the top level, then you need to do the following:

in your Gemfile (tell bundler not require sinatra):

gem 'sinatra', :require => false

and in the app's file (explicitly require sinatra):

require 'rubygems'
require 'bundler'
Bundler.require
require 'sinatra'

get '/' do
  'hello world'
end
夜司空 2024-08-17 20:49:36

要将捆绑器与 Sinatra 应用程序一起使用,您只需要做两件事。首先,创建一个 Gemfile。

gem 'sinatra'

然后,设置您的 config.ru 文件以在加载您的 Sinatra 应用程序之前加载该包。

require 'rubygems'
require 'bundler'

Bundler.require

require './my_sinatra_app'
run MySinatraApp

使用rackup启动您的开发服务器,Sinatra将通过Bundler加载。

rackup

源捆绑器文档

To use bundler with a Sinatra application, you only need to do two things. First, create a Gemfile.

gem 'sinatra'

Then, set up your config.ru file to load the bundle before it loads your Sinatra app.

require 'rubygems'
require 'bundler'

Bundler.require

require './my_sinatra_app'
run MySinatraApp

Start your development server with rackup, and Sinatra will be loaded via Bundler.

rackup

source bundler docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文