How to reference a local gem from a ruby script? - ruby

I need to reference a local gem from a plain ruby script, without installing the gem. On the trail of How to refer a local gem in ruby?, i tried creating a Gemfile with the following setup:
%w(
custom_gem
another_custom_gem
).each do |dependency|
gem dependency, :path => File.expand_path("../../#{dependency}", __FILE__)
end
and the script looks like this:
require 'custom_gem'
CustomGem::Do.something
When I execute this with:
bundle exec ruby script.rb
I get:
script.rb:1:in `require': cannot load such file -- custom_gem (LoadError) from script.rb:1:in `<main>'
If I leave out the require 'custom_gem' , I get:
script.rb:3:in `<main>': uninitialized constant CustomGem (NameError)
I even tried without bundler, and just writing gem ... :path =>̣ ... in the script itself, but without results. Is there any other way of referencing custom gems from ruby scripts, without installing the gems locally?

Make sure that your gem name as same as in Gemfile (e.g. custom_gem)
# Gemfile
source "https://rubygems.org"
gem "custom_gem", path: "/home/username/path/to/custom_gem"
Don't forget to actually install this gem using bundler
bundle install
After that, the script should be ready to use by bundle exec ruby script.rb
# script.rb
require 'custom_gem'
CustomGem::Do.something

Without using a Gemfile, you can install a local version of a gem by running bundle exec rake install in the gem's root directory and then you can reference it just like any other installed gem.

Related

Ruby - Cannot use locally installed gem

I've written a simple PasswordGenerator gem that I have at ~/workspace/gems/password_generator and have an app at ~/workspace/rubysamples/app where I want to use it. I have a Gemfile, the content of it is this:
gem 'password_generator', path: '~/workspace/gems/password_generator'
I installed it locally, like this:
bundle install --local
Resolving dependencies...
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `~/workspace/gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
It looks like it's installed locally:
bundle info password_generator
* password_generator (0.1.0)
Summary: Simple password generator
Homepage: https://github.com/jedrekdomanski/password_generator
Path: /home/jedrek/workspace/gems/password_generator
When I try to use it
~/workspace/rubysamples/app/password_reset.rb
PasswordGenerator.generate
I get an error
uninitialized constant PasswordGenerator (NameError)
What am I doing wrong? Am I missing anything?
Here's my gem repo: https://github.com/jedrekdomanski/password_generator
I also tried pointing to my repo and branch in the Gemfile
gem 'password_generator', git: 'git#github.com:jedrekdomanski/password_generator.git', branch: 'master'
but I get the same error message uninitialized constant PasswordGenerator (NameError)
There are potentially two issues. The first is how you are starting Ruby and the second is how you are requiring your module.
First, if you are starting Ruby by running ruby password_reset.rb then you are ignoring the Gemfile. The Gemfile is only used when you're using bundler, so you want to make sure you are starting Ruby by running bundle exec ruby password_reset.rb. This causes bundler to read your Gemfile and execute Ruby in that context.
Second, you're not properly including your module in your Ruby file. Just because you've added the gem to your Gemfile and started Ruby using bundler doesn't mean that the Ruby process knows you intend to use that gem's module; it just makes the module available for use. You might wonder, "Why don't I have to do that in Rails?" Because Rails does that for you automatically via config/application.rb.
Given these two issues, the correct way to accomplish your goal is to configure your app as follows:
First, create your Gemfile:
# Gemfile
gem 'password_generator', path: '~/workspace/gems/password_generator'
Second, create your password_reset.rb file:
# password_reset.rb
# Manually require any libraries that this app will use, even if defined in Gemfile
require 'password_generator'
# Call `puts` so something is printed to the console when this app runs
puts PasswordGenerator.generate
Third, run bundle install to ensure your Gemfile is properly formatted and to generate your Gemfile.lock:
⇒ bundle install
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `../../gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Fourth, run bundle exec ruby password_reset.rb and observe the output:
⇒ bundle exec ruby password_reset.rb
kpiDfyTxtdAsKmYuZqmK
Everything works because:
Ruby is started with Bundler
Bundler reads your Gemfile and makes the gems available to Ruby
Your app requires the module from the gem before attempting to use the module

Using rspec with a local gem

I am using a local gem (on my machine) with another application that is a command line app.
I have something like this in the gemfile to refer to the local gem:
gem 'mygem', :path => '/Users/devmachine/Projects/mygem'
When I run bundle console I am able to use the gem and all is well. However, whenever I run my test suite (rspec) I get the following message:
ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mygem (LoadError)
I'm confused. Any ideas?
You need to run:
bundle exec rspec
If you are not using the bundle evironment rspec won't know what you put in your Gemfile and just use the Gems it finds installed.

Can't load a gem I created

I'm trying to build my first gem. Using Ryan Biggs' tutorial as my guide, I did the following:
1) Created the gem scaffolding:
$ bundle gem hello_world
2) Edited the lib/hello_world.rb file:
require "hello_world/version"
module HelloWorld
def hi
"Hello world!"
end
end
3) Installed the gem via bundler:
$ cd hello_world
$ bundle install
At this point, if I run
$ bundle show hello_world
it shows
/Users/ykessler/gems/hello_world
so it looks like it installed.
But when I try to require the gem from irb:
require '/Users/ykessler/gems/hello_world'
it can't load it:
2.0.0-p195 :003 > require '/Users/ykessler/gems/hello_world'
LoadError: cannot load such file -- /Users/ykessler/gems/hello_world
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):3
from /Users/ykessler/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'
Where am I going wrong?
You need to run gem build hello_world.gemspec
Then to install it, you run gem install hello_world from the root of your gem project. That will install your local gem using the .gem file that we just created in your directory (not the gem from rubygems.org if it exists).
Now, if you run gem list, you should see it. You should now be able to require your gem and and access your library from other ruby code. All you have to write is require 'hello_world'. There is no need to type the full path. In fact, that's a bad idea.
This is all explained pretty clearly in the rubygems.org documentation (http://guides.rubygems.org/make-your-own-gem/). It's very clear, helpeful, and it's where I learned how to make my first gem.

How to install Cucumber using Bundler in Windows 7

I have created a gem file similar to below, I'm trying this in windows 7 environment
source :rubygems
group :test do
gem 'cucumber', '1.2.1'
gem 'rspec-expectations', '2.11.2'
end
This is executed in cmd "C:\mysite\ruby test.rb"
System throw following error msg,
test.rb:1:in '<main>' : undefined method 'source' for main:Object (NoMethodError)
I have changed the first line as
source 'http://rubygems.org/'
But still I getting same error. Really appreciate if anyone can give instructions, I'm very new to cucumber & ruby
First install bundler with
gem install bundler
Then go into your project directory. The gem file must be named Gemfile. Then you can just do
bundle install
Also, you should use HTTPS:
source 'https://rubygems.org/'
it seems that you have put "source 'http://rubygems.org/' " line in
'test.rb', which is not allowed, you must have the file Gemfile and
put this code in Gemfile
source 'https://rubygems.org/
group :test do
gem 'cucumber', '1.2.1'
gem 'rspec-expectations', '2.11.2'
end
and do bundle install

how do I require 'rake' to be able to use FileList in .gemspec?

I am using :path => '/path/to/gem' functionality of bundler to build and use a modified upstream gem, which uses Rake::FileList in its .gemspec.
At this stage, the bundle I'm installing is not yet activated, or maybe the order things are installed forbids bundler from using rake.
I am using ruby 1.8.7.
My Gemfile:
source 'http://rubygems.org'
gem "rake"
gem "foreign_gem", :path => '/home/user/src/foreign_gem'
The error I get:
$ bundle install
Unfortunately, a fatal error has occurred. Please see the Bundler
troubleshooting documentation at http://bit.ly/bundler-issues. Thanks!
/home/ilya/src/foreign_gem/foreign_gem.gemspec:11: uninitialized constant FileList (NameError)
from /home/user/.rbenv/versions/1.8.7-p358/lib/ruby/site_ruby/1.8/rubygems/specification.rb:426:in 'initialize'
from /home/user/src/foreign_gem/foreign_gem.gemspec:1:in 'new'
from /home/user/src/foreign_gem/foreign_gem.gemspec:1
You should be able to add require 'rake' at the top of your foreign_gem.gemspec file in order to use FileList.
I don't know if this is a best practice, but it should work.

Resources