Problem with RVM and gem that has an executable - ruby

I've recently made the plunge to use RVM on Ubuntu.
Everything seems to have gone swimmingly...except for one thing. I'm in the process of developing a gem of mine that has a script placed within its own bin/ directory, all of the gemspec and things were generated by Jeweler.
The bin/mygem file contains the following code: -
#!/usr/bin/env ruby
begin
require 'mygem'
rescue LoadError
require 'rubygems'
require 'mygem'
end
app = MyGem::Application.new
app.run
That was working fine on the system version of Ruby.
Now...recently I've moved to RVM to manage my ruby versions a bit better, except now my gem doesn't appear to be working.
Firstly I do this: -
rvm 1.9.2
Then I do this: -
rvm 1.9.2 gem install mygem
Which installs fine, except...when I try to run the command for mygem
mygem
I just get the following exception: -
daniel#daniel-VirtualBox:~$ mygem
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- mygem (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from /home/daniel/.rvm/gems/ruby-1.9.2-p136/gems/mygem-0.1.4/bin/mygem:2:in `<top (required)>'
from /home/daniel/.rvm/gems/ruby-1.9.2-p136/bin/mygem:19:in `load'
from /home/daniel/.rvm/gems/ruby-1.9.2-p136/bin/mygem:19:in `<main>'mygem
NOTE: I have a similar RVM setup on MAC OSX and my gem works fine there so I think this might be something to do with Ubuntu?

Using:
rvm 1.9.2 gem install mygem
is different than how I do my installs of gems inside RVM.
Try:
rvm 1.9.2
gem install mygem
You might also want to try doing gem pristine mygem which will tell Gems to remove the executable and recompile it for the current Ruby.
Another thought: Were you previously using Ruby 1.8+, and just changed to Ruby 1.9+? In Ruby 1.9 the require acts differently when loading modules that are relative to the calling code, say, in a child directory, because '.' was removed from the search path. require_relative was added to give us that capability.

Does doing export RUBYOPT=rubygems help?

Related

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 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.

Problems with ruby 'unroller' gem

I'm having a problem using unroller. I have installed the gem and wrote this simple program to help focus on the problem i'm having:
#!/usr/bin/ruby
require 'rubygems'
require 'unroller'
Unroller::trace
def foo(p1, p2)
puts p1
puts p2
end
foo("param1", "param2")
Running the program yields:
/Library/Ruby/Gems/1.8/gems/facets-2.9.3/lib/core/facets/filetest/separator_pattern.rb:5: warning: already initialized constant SEPARATOR_PATTERN
/Library/Ruby/Gems/1.8/gems/facets-2.9.3/lib/core/facets/string/bracket.rb:3: warning: already initialized constant BRA2KET
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require': no such file to load -- facets/methodspace (LoadError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
from /Library/Ruby/Gems/1.8/gems/unroller-1.0.0/lib/unroller.rb:4
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:60:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:60:in `require'
from ./ut:4
My ruby version is ruby 1.8.7 (2011-12-28 patchlevel 357). I also installed ruby on my Windows development box and get the same error and that ruby version is 1.9.3 so it does not appear to be related to the version of Ruby I'm on.
Anyone have any ideas?
Thanks very much in advance!
jon
This is a bug of unroller gem, described here: https://github.com/TylerRick/unroller/issues/1. unroller automatically requires the latest version of facets gem and the version 2.9 breaks it. (BTW gems should never use '>=' when loading dependencies, that's why '~>' is for.)
It's not that difficult to hotfix locally by using bundler and hardcoding facets gem to specific version before requiring unroller (so the specific facets version gets loaded instead of latest 2.9).
create Gemfile:
source 'http://rubygems.org'
gem 'facets', '2.8.4'
gem 'termios' # you're gonna need this gem too, for some reason
gem 'unroller'
run bundle install and then either run the script by bundle exec ruby test.rb or require bundler/setup in it:
require 'rubygems'
require 'bundler/setup'
require 'unroller'
...
UPDATE: or if you don't wanna deal with bundler, try this first, it could work too:
require 'rubygems'
gem 'facets', '2.8.4'
require 'unroller'
...

require 'nokogiri' issue with Ruby 1.9.2

I have made a Ruby project (not rails project) in Netbeans, and in main.rb file i have been requiring nokogiri but i am getting the following error.
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- nokogiri (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from /home/nadeem/rails_project/RubyApplication1/lib/main.rb:2:in `<main>'
I have installed nokogiri 1.5.0 and using ruby 1.9.2
Any idea?
Using bundler to manage your gem dependencies is probably a good idea because you can check if things are set up properly using bundle check if you've declared and successfully installed them.
The bundler setup routine can import many gems in the proper order, accounting for dependencies. Otherwise you will need to have at least this:
require 'rubygems'
gem 'nokogiri'
require 'nokogiri'
Make sure that the gems are installed with the same version of Ruby you're trying to run. It's easy to get mixed up when you're using rvm and have inadvertently added the gems to a different version.
Refer to this solution hope this will be helpful, Check your net bean's project properties and try to update path from there.
Require Nokogiri? No such file to load

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