Load error with my gem - ruby

I built a gem using
$ gem build <gemspec>
It got built successfully and I successfully installed it.
but when I do the following:
$ irb -rubygems
irb(main):003:0 require 'xxxx'
I get the following error:
LoadError: no such file to load -- xxxx
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in 'gem_original_require'
from C:/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in 'require'
What am I doing wrong?

Can you post your .gemspec file as well?
It's quite possible that you haven't included the files in the .files array. For example,
Gem::Specification.new do |s|
# Other specifications
s.files = ["bin/google", "lib/google.rb", "lib/google/utils.rb"]
s.files += ["LICENSE.md", "README.md", "google.gemspec"]
end

Related

Can't load such file - toml-rb, although the gem is installed

My Gemfile:
gem "toml-rb", "~> 0.3.8"
*.rb file:
require "toml-rb"
Locally it works, on a remote server - error:
/home/user_123/.rubies/ruby-2.4.2/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- toml-rb (LoadError)
Although I've run "bundle" and
$ bundle info toml-rb
* toml-rb (0.3.15)
Summary: TOML parser in ruby, for ruby.
Homepage: http://github.com/emancu/toml-rb
Path: /home/user_123/.gem/ruby/2.4.2/gems/toml-rb-0.3.15
What's the matter?
It looks like you're using v0.3.15 of toml-rb. At that point, it was referred to simply as toml.
Try this:
require "toml"
Here's an excerpt from the README for v0.3.15:
require 'toml'
# From a file!
path = File.join(File.dirname(__FILE__), 'path', 'to', 'file')
TOML.load_file(path)
Beginning at v1.0.0, the toml-rb would be required like so: require 'toml-rb'. Here's the release where this was changed: https://github.com/emancu/toml-rb/releases/tag/v1.0.0

ruby gem require_relative cannot load such file

I've looked around online and on SO and most of the answers to this question say that I should be using require_relative but I am using it so I don't know what the problem might be. I'm trying to build a ruby gem and my folder structure looks like this
--xmlmc-rb/
--lib/
--xmlmc-rb/
api.rb
interface.rb
version.rb
xmlmc-rb.rb
Within xmlmc-rb.rb I am requiring all three of the files under the xmlmc-rb/ directory like this
require_relative "xmlmc-rb/version"
require_relative "xmlmc-rb/interface"
require_relative "xmlmc-rb/api"
require 'net/http'
require 'nokogiri'
require 'base64'
But I keep getting an error
/Library/Ruby/Gems/2.0.0/gems/xmlmc-rb-0.1.1/lib/xmlmc-rb.rb:2:in `require_relative': cannot load such file -- /Library/Ruby/Gems/2.0.0/gems/xmlmc-rb-0.1.1/lib/xmlmc-rb/interface (LoadError)
from /Library/Ruby/Gems/2.0.0/gems/xmlmc-rb-0.1.1/lib/xmlmc-rb.rb:2:in `<top (required)>'
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:128:in `require'
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:128:in `rescue in require'
from /Library/Ruby/Site/2.0.0/rubygems/core_ext/kernel_require.rb:39:in `require'
from xmlmc_test.rb:1:in `<main>'
When I manually copy all the files into the main rb file everything works. Initially I thought it was a naming error since the casing of the files under xmlmc-rb/ weren't uniform, but I fixed that, made them all lowercase names and the same error persists.
As I didn't understand very well your gem name, I will show you how I did:
require "password_control/scrypt/ScryptHash"
In my case I have another directory inside the password_control folder. But if my file was in the password_control directory, the require would be:
require "password_control/ScryptHash"
I got this working by copying everything out of interface.rb deleting the file and recreating it. Strange why that happened.
I had the same error building a Ruby Gem.
I built and installed the gem:
gem build <gemname>.gemspec
gem install <gemname>-0.0.1.gem
I checked where the gem was installed:
gem env home
gem list -d
I tried to load the gem in Interactive Ruby (IRB):
irb
require '<gemname>'
It resulted in error:
LoadError: cannot load such file -- /Users/<username>/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/<gemname>-0.0.1/lib/my_gem_subdirectory/module_name
I was using require_relative './my_gem_subdirectory/module_name' in Ruby files in the lib directory to import modules and classes from the sub-directories.
I solved the error by updating the Gemspec .gemspec to include all files and subdirectories. Initially I only had s.files = ['lib/<gemname>.rb']. But I read the documentation http://guides.rubygems.org/specification-reference/#files and changed it to:
Gem::Specification.new do |s|
...
s.files = ['lib/discrete_math.rb']
s.files += Dir['lib/*.rb']
s.files += Dir['lib/my_gem_subdirectory/**/*']
...
end

when developing a gem with a executable binary fails to require files that live in the lib dir

I am developing a gem called I19. It is supposed to have a CLI. So in bin/i19 and I require "i19".
And from the file lib/i19.rb I do require files files placed in lib/i19/.
If I do bundle console everything works, but when I try to execute the binary file (I'm doing rake install; i19 help) I get this error: require': cannot load such file -- i19/scanners/pattern_scanner (LoadError)
This is how my files look like:
# bin/i19
#!/usr/bin/env ruby
require "thor"
require "i19"
module I19
class CLI < Thor
end
end
I19::CLI.start
# lib/i19.rb
require "i19/version"
require "i19/commands"
require "i19/scanners/pattern_scanner"
require "i19/scanners/pattern_with_scope_scanner"
module I19
end
# lib/scanners/pattern_scanner.rb
require 'i19/scanners/base_scanner'
module I19::Scanners
class PatternScanner < BaseScanner
# ...
end
end
I don't understand why it works from the console but it doesn't from the command line.
Ok I found the answer myself. The problem was not with the ruby code but the way the gem gets compiled.
My gemspec file looks like this
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'i19/version'
Gem::Specification.new do |spec|
# ...
spec.files = `git ls-files`.split($/)
# ...
end
the problem is that git ls-files is not listing untracked files. Once I did git add lib/i19/scanners/pattern_scanner.rb it worked.
It makes me then think that my workflow of manually testing the CLI might not be the best, ie rake install; i19 update . Is there a better way?

How to Require a Custom Gem

I'm writing a gem that depends on another gem I've created.
In my host gem, I'm requiring my gem as a dependency like this:
$:.push File.expand_path('../lib', __FILE__)
Gem::Specification.new do |s|
s.require_path = "lib"
s.files = Dir["lib/**/*"]
s.test_files = Dir["spec/**/*"]
s.add_dependency "my_other_gem"
end
My gemfile looks like this:
source "http://rubygems.org"
gem 'my_other_gem' path: '../my_other_gem', require: 'my_other_gem'
gemspec
And inside the host gem, I've got a class that requires my_other_gem:
require 'my_other_gem'
In my_other_gem, inside lib/my_other_gem.rb, I've got two more require classes. So it looks like this:
require 'my_other_gem/foo'
require 'my_other_gem/bar'
When I spin up IRB in the host app and run require 'my_other_gem', I get this error
LoadError: cannot load such file -- my_other_gem
When I'm playing in the my_other_gem directory and I spin up IRB, the same require 'my_other_gem' command does not error out. Everything runs normally. But for some reason I can't require my_other_gem when I'm in my host gem.
What step am I missing?
How do you start irb? You need to run it in the bundler context with bundle exec.
I just tried and if I just run irb, I get the same error.
But if I run bundle exec irb, it works.

Can't use created gem on computer

I created a gem with the following gemspec file.
$:.push File.expand_path("../lib", __FILE__)
Gem::Specification.new do |s|
s.name = 'SomeToken'
s.version = '0.0.0'
s.date = '2013-08-04'
s.summary = "A gem for use with SomeToken."
s.description = "A gem for use with SomeToken."
s.authors = ["Jason Tanner"]
s.email = 'jasontanner328#gmail.com'
s.files = Dir.glob '**/*'
s.homepage = ''
s.license = ''
end
Then in my terminal I run
gem build sometoken.gemspec
Then,
gem install ./SomeToken-0.0.0.gem
The gem is successfully installed, so when I run irb and I run...
require 'SomeToken'
Which responds with the error
LoadError: cannot load such file -- SomeToken
from /Users/jason/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
from /Users/jason/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
from (irb):1
from /Users/jason/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>'
I've tried changing the casing for the string, in numerous combinations but still get the same error. What's wrong with my gem and how can I fix it?
Try require 'some_token'.
Using require in general
require takes the name of a ruby file, not the name of a gem. For example, if you have the following directory structure
- foo.rb
- main.rb
Then in main.rb, you can use require 'foo' to use stuff from foo.rb.
Using require with gems
Notice that the first line of your gemspec has $:.push File.expand_path("../lib", __FILE__). This adds the lib directory of your gem to the search path. Thus, if you have
lib/
some_token.rb
then you should use require 'some_token'.
I don't know if Ruby 2.0.0 have this 'bug', but some Ruby versions must have a
require 'rubygems'
before you require any gem.
Guess it's worth a try :)

Resources