I'm only need sinatra/base, but I don't know how to do it by using bundler. Because in the Gemfile I can't give it 'sinatra/base', but the gem name sinatra instead.
Any idea?
Use the following syntax in your Gemfile:
gem "sinatra", "X.Y.Z", :require => "sinatra/base"
You can then require it via bundler using the #require method:
Bundler.require :default
Related
Trying to figure out Ruby's Bundler library. My understanding is we can require only certain groups, but my setup seems to be loading gems specified in other groups as well.
Gemfile
source 'https://rubygems.org'
# Specify your gem's dependencies in apple.gemspec
gemspec
group :production do
gem 'mail'
gem 'bundler'
gem 'pry'
gem 'commander'
gem 'fastlane'
gem 'spaceship'
gem 'highline'
gem 'terminal-table'
gem 'clipboard'
gem 'date'
gem 'mysql2'
gem 'fileutils'
gem 'redis'
gem 'json'
gem 'logger'
gem 'jira-ruby', :require => 'jira-ruby'
end
group :jenkins do
gem 'terminal-table'
gem 'pry'
gem 'mail'
gem 'jira-ruby'
gem 'spaceship'
end
test.rb
require 'pry'
binding.pry
require 'rubygems'
require 'bundler/setup'
Bundler.require(:jenkins) # I want this step to require only gems listed under 'jenkins' group in `Gemfile`.
...
When I run the code, it seems Bundler.require(:jenkins) step seems to be requiring all gems specified in Gemfile,
I am still in the process of understanding Bundler, pardon me if the question I asked is too obvious. Does anyone know how to only load the gems from bundler groups? Thanks in advance!!
This is kind of confusing, but as far as I can tell from using it myself, it only requires the gems in the given group even though it lists all of them.
It's pretty easy to see this for yourself, just try using one from the production group
Bundler.require(:jenkins)
Date.new # Should error
Really it seems like you may have noticed this yourself, but you just didn't realize.. If it was including all of them you wouldn't have needed to require pry in test.rb :)
I'm doing some messing around to try to understand better how bundler works. I have just three files in my working directory:
Gemfile Gemfile.lock test.rb
All the Gemfile has is gem "slop" and test.rb looks like this:
puts Slop.parse
When I run bundle exec test.rb I get a NameError due to not having a require statement:
[ec2-user#xx my_app]$ bundle exec ruby test.rb
test.rb:1:in `<main>': uninitialized constant Slop (NameError)
But if I run bundle console, Bundler loads the gem correctly and I can run Slop.parse from the console without having to explicitly type require "slop":
[ec2-user#xx my_app]$ bundle console
irb(main):001:0> Slop.parse
=> #<Slop::Result:0x00000001339838...
So what am I missing? I was under the impression that since I don't have require: false in my Gemfile, Slop should be loaded when I run bundle exec ruby test.rb and I shouldn't need to put the require "slop" line in the file.
You need to config bundler to require all gems on your Gemfile like this:
require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
Check the docs at http://bundler.io/v1.12/bundler_setup.html
I was under the impression that since I don't have require: false in
my Gemfile, Slop should be loaded when I run bundle exec ruby test.rb
and I shouldn't need to put the require "slop" line in the file.
The bundler docs say:
Specify your dependencies in a Gemfile in your project's root:
source 'https://rubygems.org'
gem 'nokogiri' #<======HERE
Inside your app, load up the bundled environment:
require 'rubygems'
require 'bundler/setup'
# require your gems as usual
require 'nokogiri' #<========AND HERE
As for this:
I was under the impression that since I don't have require: false in
my Gemfile, Slop should be loaded when I run bundle exec ruby test.rb
and I shouldn't need to put the require "slop" line in the file.
The bundler docs are horrible on this point. As far as I can tell, :require => false is a Rails specific thing, which is used to decrease load times on project startup. In a rails app, specifying require: false means that the gem won't be loaded until you manually require the gem. If you don't specify :require => false, then the gem will be loaded automatically--however that is because rails code is written to do that automatic loading. Your app has no code that performs a similar function.
Edit: Made a mistake while testing. So here's the way it works: In a non rails app, e.g. in your test.rb, if you want to automatically require all the gems specified in your Gemfile, you need to write:
Bundler.require :default
The Bundler docs mention that in the fine print here:
Specify :require => false to prevent bundler from requiring the gem,
but still install it and maintain dependencies.
gem 'rspec', :require => false
gem 'sqlite3'
In order to require gems in your Gemfile, you will need to call Bundler.require in your application.
I'm not sure why that requirement was only mentioned in conjunction with require: false instead of being stated at the outset.
And, in your Gemfile if you specify:
gem 'slop', :require => false
(as well as Bundler.require :default in test.rb), then you also have to explicitly require the slop gem in test.rb:
require 'slop'
In other words, Bundler.require :default automatically requires all the gems in your Gemfile except the gems marked with require: false. For gems marked with require: false, you have to manually write require 'gem_name' in your app.
Therefore, neydroid posted the correct solution.
* In your Gemfile, you can nest gems in groups, which affects how Bundler.require() works. See the Bundler docs on groups.
You should add the require "slop" inside your test.rb
I'm trying to deploy an app in Openshift that requires some gems, how do I get Openshift to install those when I push via git?
Here's what my config.ru file currently looks like:
require 'rubygems'
require './app.rb'
run Sinatra::Application
And as for app.rb it requires the following gems:
require 'sinatra'
require 'redcarpet'
require 'stringex'
require 'data_uri'
Any ideas what I'm doing wrong here? Thanks in advance!
You probabily need to add those into a Gemfile like:
# Gemfile
source 'http://rubygems.org'
gem 'sinatra'
gem 'redcarpet'
gem 'stringex'
gem 'data_uri'
and run bundle locally before pushing to generate the Gemfile.lock
Use Bundler for you app
in Gemfile
gem 'sinatra'
gem 'redcarpet'
gem 'stringex'
gem 'data_uri'
in config.ru
require 'rubygems'
require 'bundler'
Bundler.require
require './my_app'
run Sinatra::Application
Start server with rackup, and Sinatra will be loaded via Bundler.
$ rackup
I'm trying to run a Sinatra application with the most basic app.rb:
require 'sinatra/activerecord/rake'
require 'bundler/setup'
Bundler.require(:default)
require_relative './config'
require_relative './models/star'
require_relative './models/planet'
require_relative './models/moon'
require_relative './models/astronaut'
get '/' do
erb :index
end
After using Bundle and creating Gemfile.lock I'm keep getting this error:
You have already activated activesupport 4.0.2, but your Gemfile requires activesupport 3.2.16. Using bundle exec may solve this. (Gem::LoadError)
My Rakefile is:
source 'https://rubygems.org'
gem 'sinatra'
gem 'sinatra-reloader'
gem 'sinatra-activerecord'
gem 'activerecord', '~> 3.2.13'
gem 'rake'
gem 'pg'
gem 'pry'
I'll be grateful for any suggestions.
You have both ActiveRecord 4.0.2 and 3.2.13 installed on your system. The first line of your app requires sinatra/activerecord/rake which in turn requires activerecord, without specifying which version. This activates and loads 4.0.2 – the latest version.
In the next line you try to set up Bundler. Bundler now tries to activate version 3.2.13 of ActiveRecord as specified in your Gemfile, but can’t since a version is already activated, so you get an error.
To fix, simply make sure you call require 'bundler/setup' first, before you require any other files. This will ensure any files you require will be compatible with your Gemfile.
Alternatively you could remove the call to require bundler/setup and make sure you always start your app using bundle exec.
I am using bundler to require all the gems in my project. However, it's not working for yaml/logger.
If I add gem 'yaml' to my gemfile, and run bundle install, I get:
Could not find gem 'yaml (>= 0) ruby' in the gems available on this machine.
But I require it normally just fine. What am I doing wrong?
Thanks
YAML is part of the Ruby Standard Library, and not a Gem.
You do not need to add it to your Gemfile, just require it.
irb(main):001:0> require 'yaml'
=> true
irb(main):002:0> YAML
=> Psych
The same applies for Logger.