Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a gem in a pure ruby script (not Rails)

A ruby file:

gem "my-gem", git: "https://github.com/gem123.git", branch: "some-branch"
require "my-gem"

var1 = SomeGem::some_method123
puts var1

It says Could not find 'my-gem' (>= 0) among 330 total gem(s) (Gem::LoadError). Why not? I need a special branch of a gem and don't want to clone the repository.

like image 268
Incerteza Avatar asked Aug 18 '14 23:08

Incerteza


People also ask

Is Rails a ruby gem?

RoR Gems make development with Rails worthwhile. Gems in Rails are libraries that allow any Ruby on Rails developer to add functionalities without writing code. You can also call Ruby on Rails gems as plugins for adding features. A Ruby gem enables adding features without creating the code again and again.

Is RubyGems included with Ruby?

Ruby comes with RubyGems by default since version 1.9, previous Ruby versions require RubyGems to be installed by hand. Some other libraries are released as archived (.

How does Ruby gem work?

Gems can be used to extend or modify functionality in Ruby applications. Commonly they're used to distribute reusable functionality that is shared with other Rubyists for use in their applications and libraries. Some gems provide command line utilities to help automate tasks and speed up your work.

Where is GEM file in Ruby?

A Gemfile describes the gem dependencies required to execute associated Ruby code. Place the Gemfile in the root of the directory containing the associated code. For instance, in a Rails application, place the Gemfile in the same directory as the Rakefile .


1 Answers

Use bundler. Create a Gemfile along side your ruby script.

In the Gemfile, add:

gem "my-gem", git: "https://github.com/gem123.git", branch: "some-branch"

Make sure bundler is installed:

gem install bundler

And install the required gems:

bundle install

Now just initialize bundler at the top of your script:

require 'rubygems'
require 'bundler/setup'

# require your gems as usual
require 'my-gem'
like image 117
infused Avatar answered Sep 20 '22 07:09

infused