Ruby require 'some-gem' works in console, not in Eclipse? - ruby

Well, I'm trying to run a simple web server through "rack". So this is my program:
require 'rubygems'
require 'rack'
class HelloWorld
def call(env)
[200, {"Content-Type" => "text/html"}, ["Hello Rack!"]]
end
end
Rack::Handler::Mongrel.run HelloWorld.new, :Port => 9292
If I run it in the console, it works fine. If I run it in Eclipse, it ends up with error:
/Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- rack (LoadError)
from /Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/MY_SUPER_SECRET_USER/Sites/service/service.rb:2:in `<main>'
The one that is working is called like this:
MY_SUPER_SECRET_USER#MacBook-Pro:~/Sites/service $ which ruby
/Users/MY_SUPER_SECRET_USER/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
MY_SUPER_SECRET_USER#MacBook-Pro:~/Sites/service $ ruby service.rb
Then when I try to open localhost:9292 it shows the expected "Hello Rack" code.
I'm on Mac OS X 10.8, with ruby 1.9.3 installed through rvm (this must be obvious). My "rack" package has been installed with sudo gem install rack
So as you can see, the Eclipse is configured with the very same ruby executable. Any suggestions will be of great help!

The line
custom_require.rb:36:in `require': cannot load such file -- rack (LoadError)
means it can't find where you installed the rack gem. Reading around the internet, I see again and again using sudo doesn't seem to work. Try just installing it and see if that fixes it.
$ gem install rack (no sudo)

I ran into this same problem, trying to require gems in Eclipse which it wasn't recognizing, even though everything seemed to be configured correctly (the gems were installed, Eclipse was pointing at the right Ruby interpreter, etc).
I ended up making it work by adding GEM_HOME and GEM_PATH variables to the Environment tab of Debug/Run Configurations.
More detailed answer here: https://stackoverflow.com/a/28419300/525338

Related

rvm does not import gem path

I’m running on Mac OS X Yosemite 10.10 Beta 3, I did a fresh install of rvm (removed everything I could think of and reinstalled the whole thing.
Attempting to run a scripts I had working on Mavericks.
Maxims-MacBook-Air:AppleSampleCodeWorker maximveksler$ gem install restclient
Successfully installed restclient-0.10.0
Parsing documentation for restclient-0.10.0
Done installing documentation for restclient after 0 seconds
1 gem installed
Maxims-MacBook-Air:AppleSampleCodeWorker maximveksler$ which irb
/Users/maximveksler/.rvm/rubies/ruby-2.1.2/bin/irb
Maxims-MacBook-Air:AppleSampleCodeWorker maximveksler$ irb
2.1.2 :001 > require 'restclient'
LoadError: cannot load such file -- restclient
from /Users/maximveksler/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/maximveksler/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):1
from /Users/maximveksler/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>’
Path looks normal
Maxims-MacBook-Air:AppleSampleCodeWorker maximveksler$ env | grep GEM
GEM_HOME=/Users/maximveksler/.rvm/gems/ruby-2.1.2
GEM_PATH=/Users/maximveksler/.rvm/gems/ruby-2.1.2:/Users/maximveksler/.rvm/gems/ruby-2.1.2#global
Also the gem is installed
Maxims-MacBook-Air:AppleSampleCodeWorker maximveksler$ file /Users/maximveksler/.rvm/gems/ruby-2.1.2/gems/restclient-0.10.0/lib/rest_client.rb
/Users/maximveksler/.rvm/gems/ruby-2.1.2/gems/restclient-0.10.0/lib/rest_client.rb: ASCII C++ program text
So what am I missing ?
Looking at your link here, there is an error in your syntax. It should be require 'rest_client' rather than require 'restclient'. The reason that your version still works is as you said because there is a file called restclient.rb which is used as the source for the additional restclient binary that the gem supplies (which is against regular naming convention, you should file an issue with the github).
Now because you require this file (restclient.rb) which is used to set up an environment that already has RestClient available, it is effectively the same as requiring rest_client. It may however have unintended consequences so you should probably stick to the convention outlined in the documentation.
This is all from reading the documentation here and glancing at the files here. Does that make sense? I worried it was unclear as the two files are very similar.

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.

WEBrick CGI handler pointing to wrong gem location

I am trying to execute my CGI program from WEBrick CGIhandler and kept failing with 'loaderror' of gem libraries. Is someone seen following behavior before? How could I get my CGI scripts to search valid gem location?
[Situaltion]
When I ran CGI scripts that requires 'mysql2' library via Webrick server, I got a following error:
ERROR CGIHandler: /home/charles/code/svr/lib/cgitest.rb:
/home/charles/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mysql2/version (LoadError)
Comment this particular 'require' out from cgitest.rb makes this script work so looks just failed to load this library. Added "puts Gem.path" to the script indicates CGIHandler is looking to following directories to find gem;
["/home/charles/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/gems/1.9.1", "/.gem/ruby/1.9.1"]
while actual GEM:HOME and GEM:PATH are following;
["/home/charles/.rvm/gems/ruby-1.9.3-p194", "/home/charles/.rvm/gems/ruby-1.9.3-p194#global"]
I think I am missing something very simple but could not figure them out. Really appreciate if someone lead me to the right direction. Any input would be highly welcome.
[Environment]
Using RVM to install Ruby 1.9.3 to Ubuntu Server 12.4 with bunch of gems including "mysql2" library. And the script can load 'mysql2' when I run from local.
$which ruby
/home/charles/.rvm/rubies/ruby-1.9.3-p194/bin/ruby
$irb
1.9.3-p194 :001 > Gem.path
=> ["/home/charles/.rvm/gems/ruby-1.9.3-p194", "/home/charles/.rvm/gems/ruby-1.9.3-
p194#global"]
1.9.3-p194 :002 > require 'mysql2'
=> true
You need to use wrapper instead of bin/ruby:
/home/charles/.rvm/wrappers/ruby-1.9.3-p194/ruby
This will ensure you load proper environment when starting ruby.

Problem with RVM and gem that has an executable

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?

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