Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra + Bundler?

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

like image 627
khelll Avatar asked Nov 10 '09 07:11

khelll


People also ask

What is the use of bundler?

Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that you need. Bundler prevents dependencies and ensures that the gems you need are present in development, staging, and production.

Is bundler part of Ruby?

As of Ruby 2.6. 0preview3, Bundler is part of core Ruby.

What is bundler in Ruby on Rails?

In Rails, bundler provides a constant environment for Ruby projects by tracking and installing suitable gems that are needed. It manages an application's dependencies through its entire life, across many machines, systematically and repeatably. To use bundler, you need to install it.


2 Answers

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.

like image 84
Ryan McGeary Avatar answered Sep 24 '22 12:09

Ryan McGeary


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 
like image 20
khelll Avatar answered Sep 23 '22 12:09

khelll