failed to require a local gem - ruby

created a demo gem
built it :
gem build control.gemspec
Successfully built RubyGem
Name: process_controller
Version: 0.0.1
File: process_controller-0.0.1.gem
installed it:
gem install --local process_controller-0.0.1.gem
Successfully installed process_controller-0.0.1
1 gem installed
but I can't seem to require it :
irb (from the installed directory)
2.1.5 :001 > require 'process_controller'
LoadError: cannot load such file -- process_controller
from /Users/partuck/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/partuck/.rvm/rubies/ruby-2.1.5/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from (irb):1
from /Users/partuck/.rvm/rubies/ruby-2.1.5/bin/irb:11:in `<main>'
also tried
require 'control' # since it's in the control directory
bundle show output:
bundle show
Gems included by the bundle:
* bundler (1.7.9)
* process_controller (0.0.1)
here is a screen shot of the directory structure
http://cl.ly/image/2t1s331e273u

turns out you can't require the gem, you have to require the main file name.
in my case its:
require 'control'
thanks a lot #matt

Related

travis failing to run rake with LoadError: cannot load such file -- rspec/core/rake_task

Let me preface this with this all runs locally and succeeds.
Source code repo
Travis log for job #4.1
Error:
$ rake
rake aborted!
LoadError: cannot load such file -- rspec/core/rake_task
/home/travis/build/alienfast/gulp-pipeline-rails/Rakefile:4:in `<top (required)>'
/home/travis/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
/home/travis/.rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
.travis.yml
language: ruby
rvm:
- 2.2.2
- ruby-head
matrix:
allow_failures:
- rvm: ruby-head
cache: bundler
before_script:
# install Gulp 4 CLI tools globally from 4.0 GitHub branch
- npm install gulpjs/gulp-cli#4.0 -g
script: rake
Gemfile
gemspec
# travis-ci quirk?
group :development, :test do
gem 'rake'
gem 'rspec'
gem 'rspec-rails'
gem 'capybara'
gem 'capybara-webkit'
end
Fix attempts:
Locally, I have emptied my rvm gemset, deleted locks, and replicated the travis build - still works
I tried moving the necessary test gems from the gemspec to the Gemfile
So I'm pretty confused, I have other projects that don't have this problem and I don't see anything obvious. Is there any reason to get LoadError: cannot load such file -- rspec/core/rake_task?
Have you tried using bundle exec rake -t As noted here LoadError: cannot load such file -- rspec/core/rake_task

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.

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?

Problem with gem loading

I struggle to have gems correctly loading in a Snow Leopard environment. I installed ruby and rubygems in '/usr/local' (from http://hivelogic.com/articles/compiling-ruby-rubygems-and-rails-on-snow-leopard/ instructions), I installed some gems with 'gem install gem-name'.
I correctly see the gem list:
$ gem list
*** LOCAL GEMS ***
chrisjpowers-iterm_window (0.3.2)
gemcutter (0.3.0)
I can see the gems correctly installed:
$ ls /usr/local/lib/ruby/gems/1.8/gems/
chrisjpowers-iterm_window-0.3.2
gemcutter-0.3.0
And the gem path correctly defined:
$ gem env path
/usr/local/lib/ruby/gems/1.8
And I defined various paths in my bash profile:
$ cat ~/.bash_profile
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
export GEM_HOME="/usr/local/lib/ruby/gems/1.8"
export GEM_PATH="/usr/local/lib/ruby/gems/1.8"
export RUBY_LIB="/usr/local/lib/ruby:/usr/local/lib/ruby/site_ruby"
However when I run the following script
#!/usr/local/bin/ruby
require 'rubygems'
require 'chrisjpowers-iterm_window'
I get the following error
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- chrisjpowers-iterm_window (LoadError)
from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /usr/local/bin/sp:4
I have no idea how to fix it, any help would be greatly appreciated :)
You're requiring the wrong lib name. The gem is named chrisjpowers-iterm_window because of GitHub namespacing, but the library is named iterm_window:
require 'rubygems'
require 'iterm_window'
The lib path differs from the gem path. The gem path is where the gems are installed, but the lib path contains the lib directory of every installed gem after rubygems is loaded. You should see an iterm_window.rb inside the lib directory of that gem.

Resources