Correct way to combine `bundle exec` and `ruby -r`? - ruby

Let's experiment with a random gem, it doesn't matter which.
# Gemfile
gem "hashie"
Let's install our bundle in a (semi) non-standard place, so that ruby can't find it:
bundle install --path=vendor/bundle
ruby -e 'require "hashie"'
# cannot load such file -- hashie (LoadError)
As expected, if we add bundle exec to the above, then ruby -e can find our gem:
bundle exec ruby -e 'puts require "hashie"'
# true
But, even with bundle exec, ruby -r cannot find our gem!
bundle exec ruby -r hashie -e 'puts "win"'
# cannot load such file -- hashie (LoadError)
The only way I have found to combine bundle exec and ruby -r is:
bundle exec ruby -r 'bundler/setup' -r hashie -e 'puts "win"'
# win
The documentation for bundle exec does not mention ruby -r, so I ask here, is this the correct way?

-r 'bundler/setup'
is acceptable. It means to "require all of the gems in my Gemfile".
'hashie' could not load its dependencies that is why "LoadError" occurred.
reference: bundler_setup

Related

Mysterious LoadError on installed gem with bundler

The following terminal session explains the problem. I don't understand how it's possible that I'm getting a load error on an installed gem.
The sailthru gem which isn't loading is something I installed today, which may be relevant. I verified it was installed to the same location as my other gems, and I also tried restarting the Terminal app. I'm using rbenv on a mac. However, I have not installed a new version of ruby for at least a few weeks, and have been using it daily until now without any issues.
Thanks for any help.
$ gem list | grep sailthru
sailthru (1.1.2)
sailthru-client (4.0.1, 2.0.0)
$ cat Gemfile | grep sailthru
gem 'sailthru'
$ cat Gemfile.lock | grep sailthru
sailthru (1.1.2)
sailthru
$ bundle exec thin start
Using rack adapter
/Users/jg/Dropbox/sinatra/app.rb:5:in `require': cannot load such file -- sailthru (LoadError)
$ irb
irb(main):001:0> require 'sailthru'
=> true
irb(main):002:0>
There is no sailthru file to require.
You should take a look at the project page.
require 'sailthru'
should be
require 'sailthru/client'
Hope this helps.
EDIT
Just noticed the last part where you gave irb output.
The sailthru.rb file is part of the sailthru-client gem. It is working through irb because irb is not loaded via bundle exec. This means that every gem installed is available in irb.
To fix this (if you need that sailthru-client gem in your app. Add gem 'sailthru-client' to your gem file. Do a bundle install, then run your bundle exec again.
You should not see that error after that. Keep in mind. bundle exec only uses gem libraries that are specified in your Gemfile.

How to reference a local gem from a ruby script?

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.

ruby command line LoadError

When I try to include a library on the command line, I receive LoadError messages
$ ruby -v
ruby 1.8.7 (2012-06-29 patchlevel 370)
$ gem list | grep coderay_bash
coderay_bash (1.0.2)
$ ruby -rcoderay_bash /bin/coderay -v
ruby: no such file to load -- coderay_bash (LoadError)
$ ruby -rubygems -rcoderay_bash /bin/coderay -v
ruby: no such file to load -- coderay_bash (LoadError)
It looks to work with ruby 1.9.2
$ ruby -v
ruby 1.9.2p290 (2011-07-09)
$ ruby -rcoderay_bash /bin/coderay -v
CodeRay 1.0.7
In Ruby 1.8, anything you want to require that was installed with RubyGems cannot be accessed until you require 'rubygems'. 1.9 removes this requirement.
You have several options for this:
Just put require 'rubygems' at the top of your file. This is harmless for 1.9 and is probably the easiest thing, because it's in the code and no one using your app has to remember anything
Change your shebang line to #!/usr/bin/env ruby -rubygems This tells the Ruby interpreter to require rubygems, but allows users to avoid this by sending your file to ruby directly, if they are offended by RubyGems for some reason
Always run with ruby and use -rubygems, e.g. ruby -rubygems my_app.rb This has no dependencies on RubyGems in your code, and will work, but you have to remember to do it everytime, which is somewhat of a pain.

require jruby seems to work and fail simultaneously

John-Breedloves-Mac-mini:~ john_breedlove$ irb
>> require 'jruby'
=> true
>> require 'zxing'
RuntimeError: ZXing requires JRuby
from /Library/Ruby/Gems/1.8/gems/zxing-0.1.1/lib/zxing.rb:1
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:36:in `require'
from (irb):2
>>
How is this possible?
Further, how would I write that in a ruby script, though?
I have a file I want to execute called test.rb, which contains the following:
require 'rubygems'
require 'jruby'
require 'zxing'
ZXing.decode 'test.png'
I am executing it from the command-line like so:
ruby test.rb
In this context, how do I include java? Or is this even possible?
You are using the JRuby gem, rather than the JRuby itself.
JRuby (which ZXing is checking on line 1) is a module that gets defined only after you require 'java' in JRuby.
This should be clearly stated in ZXing's documentation, but it doesn't seem to be.
Here's the test output when I run the following from cloned ZXing source code:
$ ruby -v -I lib -r zxing -e 'p 0'
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
./lib/zxing.rb:1: ZXing requires JRuby (RuntimeError)
$ jruby -I lib -r zxing -e 'p 0'
/Users/asari/Development/src/zxing.rb/lib/zxing.rb:1: ZXing requires JRuby (RuntimeError)
from /Users/asari/Development/src/zxing.rb/lib/zxing.rb:1
...internal jruby stack elided...
from (unknown).(unknown)(/Users/asari/Development/src/zxing.rb/lib/zxing.rb:1)
from (unknown).(unknown)(:1)
$ jruby -r java -I lib -r zxing -e 'p 0'
0

I see gem in "gem list" but have "no such file to load"

I am on Ubuntu10
sudo apt-get install ruby1.9.1-full
then download sources of rubygem 1.3.7 and install it
sudo ruby setup.rb
then, for example, install sinatra
sudo gem install sinatra
Finally open irb and type
require "rubygems"
require "sinatra"
and get error
LoadError: no such file to load -- sinatra
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
I had exactly this problem. The problem is that gem and ruby disagree about where the gems live. Compare these:
ruby -e "puts Gem.path"
gem env
gem which sinatra
If you're like my setup, you'll notice that there's an entry in gem env's paths that isn't in Gem.path, and that's exactly where sinatra will claim to be. In my case, I had to add
export GEM_HOME=/usr/lib/ruby/gems/1.9.1
to my .profile. Then everyone was happy.
Execute
sudo gem install sinatra --verbose
and note the path where the gem is getting installed.
Then try this in irb
puts $LOAD_PATH
and make sure that gem is installed in one of the directories in $LOAD_PATH
And ideally just start using http://rvm.beginrescueend.com/
I usually hit this error when I forget:
require 'rubygems'
It'd be helpful if you provided the actual code sample, though, what gem you want to require, and what Ruby version you're using if this doesn't solve the problem.
This was before here on SO quite a few times. Problem is that you probably have two versions of ruby. The one is installing the gem and the other one is trying to use it. Do this in terminal:
$ which -a ruby
Or this:
$ which -a gem
to see if you have more than one version of ruby/gem installed. If so - remove one version (via $ rm or package manager of your system).
I use ruby gems 1.8.7 for a project. I was getting the same error. Use the line require 'rubygems'. It must always be the first require statement, otherwise you can get an error. In my code, I had
require 'watir'
require 'rubygems'
# more code
I got the error - in `require': no such file to load -- watir (LoadError).
When I put rubygems first, the error went away and everything worked. I don't know
why this happens.
Btw, I tried user24359 answer and it did not help me.
C:\code>ruby -e "puts Gem.path"
-e:1: uninitialized constant Gem (NameError)

Resources