How to create a Gemfile? - ruby

I'm very new to Ruby.
I was following a blog post that says that in order to install a required dependencies I need to create a Gemfile.
How do I create a Gemfile with rspec as a dependency?

bundle init generates a Gemfile into the current working directory.
$ bundle init
Writing new Gemfile to /app/Gemfile
$ cat Gemfile
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "rails"

The Gemfile is just a text file within your project which contains a list of gems for use in your application.
If you do not have one in your application, you can create the file using an editor of your choice, saving it as Gemfile (with no extension), and in your example, containing:
source 'https://rubygems.org'
gem 'rspec'

A gemfile is automatically created when you start a new rails application.
type rails new appName and then it will be generated automatically. It will also be populated with some gems.
To install the gems run bundle install or simply bundle
Make sure you are requiring the gems you need. specifically rspec and jruby
group :development, :test do
gem 'rspec-rails', '~> 3.0'
end
Look at this website for more information
http://bundler.io/

Related

Optional runtime dependency for Ruby gem with executables

I'm writing a gem aipp which exposes a few executables, nothing fancy here:
#!/usr/bin/env ruby
require 'aipp'
AIPP::NOTAM::Executable.new(File.basename($0)).run
Parts of the gem optionally use database adapters (pg or ruby-mysql gem). But since these can be a pain in the butt when the gem is used on the cloud, I'd like to really make them optional and not require them as runtime dependency in the .gemspec.
So I require them conditionally at runtime:
require 'pg' if ENV['AIPP_POSTGRESQL_URL']
require 'mysql' if ENV['AIPP_MYSQL_URL']
Unfortunately, that doesn't work as expected. When either of the environment variables is set and the executable is used, the require fails – probably because there's no dependency declared.
I've tried an inline Gemfile on the executable like the following. Works in development (repo checkout), but not when the gem is intalled via Rubygems:
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'aipp'
gem 'pg', '~> 1' if ENV['AIPP_POSTGRESQL_URL']
gem 'ruby-mysql', '~> 3' if ENV['AIPP_MYSQL_URL']
end
AIPP::NOTAM::Executable.new(File.basename($0)).run
What's the correct approach to require gems which are not listed as runtime dependency but installed additionally (via gem install or Gemfile)?
Maybe somebody knows of an existing gem which has already solved this problem somehow.
Thanks for your help!
I would use bundler groups for that.
gemfile do
source 'https://rubygems.org'
gem 'aipp'
group :database do
gem 'pg', '~> 1'
gem 'ruby-mysql'
end
end
There is more info about how to use them in the link.

How to work with gemspec add_runtime_dependency and `bundle install`

I have a codebase which has a gemspec file like:
require "rubygems"
::Gem::Specification.new do |specification|
specification.add_development_dependency "yard", "~> 0.9"
specification.add_runtime_dependency "dry-validation", "~> 0.13"
end
bundle install will install both dependency types. So I want to just install the runtime dependencies for my CI scripts. I see bundle install --with <group> exists, but I don't have groups. Run interactively, the returned specification has an empty result returned from .groups. I would love to rationalize these two worlds. Must I explicitly add a group for each gem dependency? Does add_runtime_dependency and add_development_dependency even make a difference?
from bundler's documentation
Because we have the gemspec method call in our Gemfile, Bundler will automatically add this gem to a group called “development” which then we can reference any time we want to load these gems with the following line:
Bundler.require(:default, :development)
in your case, if you wish to install all rubygems that are not for development, then try
bundle install --without development
for future bundler version, you can configure it locally (or globally)
bundle config set --local without 'development'
to make it all work, verify that you have a Gemfile in your project, which will look like
# frozen_string_literal: true
source 'https://rubygems.org'
gemspec

Rubymotion, adding interface builder: running a rake task from gem gives "Don't know how to build task 'ib'"

I have a Ruby Motion project, and I want to add Interface Builder to it.
I've added the gem:
gem 'ib'
But when I run bundle exec rake ib, I get Don't know how to build task 'ib'
Does any one know what I might be doing wrong? here is my gemfile:
source "https://rubygems.org"
gem "rake"
gem "ProMotion", '~> 2.0'
gem "ProMotion-push", git: 'git#github.com:BananaNeil/ProMotion-push.git', :branch => 'actionable-push-notifications'
gem "cocoapods"
gem "motion-cocoapods"
gem 'xcodeproj'
gem "bubble-wrap-http", git: 'git#github.com:BananaNeil/BubbleWrap-HTTP.git', branch: 'allow_invalid_ssl_certs'
gem "bubble-wrap"
gem "sugarcube" # monkeypatch all the things
gem "motion-yaml"
gem "motion-stump"
gem 'houston'
# Enter debugger with simple syntax
gem 'dbt' #-----> break
# Add pretty print
gem 'motion-pp'
# Handle address book for us
gem 'motion-addressbook'
gem 'ib'
Make sure to require 'ib' inside the Rakefile, either with Bundler or manually for each gem.
And if you use Bundler, you might need to remove the begin/catch guard, because it will silence all import related errors.

Install *gem file with bundle

I try to develop a gem. I include it in my rails application through Gemfile with :path option for testing purpose, but there are some errors that appear when I build it and release to rubygems that does not appear when gem included from local path. How can I install gem from *gem file (made with rake build command) in rails or any bundle driven application for testing?
This will give you the best help: http://guides.rubygems.org/make-your-own-gem/
But in summary you need to build your gem from the .gemspec then using irb require your gem to test it out.
Example:
gem build hola.gemspec
% irb
require 'hola'
=> true

Making a Ruby project that is not a gem (or some other kind)

I'm working on a project currently that I don't want to be a gem (or some other kind of project). How would I go about setting it up so that I can still have the same compatibility requirement abilities as a gem (e.g. Gemfile dependencies) but simultaneously not be a gem (or some other kind of project)?
You have to actually try to build a gem so this is easy!
to use bundler without Rails, a gem, whatever just create a directory
mkdir my-non-gem-project
cd my-non-gem-project
install bundler
gem install bundler
and initialize your Gemfile
bundle init
that will create a Gemfile for you and you can add to it and run bundle to install the dependencies from it
The simplest way to use bundler in your project would then be to open your main app file and add
require 'bundler/setup'
Bundler.require
This will require all of the gems you have in your Gemfile in the file this is added to. I am pretty sure that this file must be in the same directory as your Gemfile. More information here
Have fun with your Ruby project!

Resources