I created a gem and installed it on my system. The gem has 2 rb files, Myfile.rb and file_two.rb, one requires another, both are in lib folder. When I'm trying to load it in irb like require 'Myfile', I get an error
- LoadError: cannot load such file -- /usr/local/lib/ruby/gems/2.0.0/gems/Myfile-0.1.0/lib/file_two
In gem folder I see only 1 file. I believe the problem is in the following snippet of gemspec:
spec.files = Dir['Rakefile', '{bin,lib}/**/*',
'README*', 'LICENSE*'] & `git ls-files -z`.split("\0")
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
Please, advice how to rewrite it correctly, so both my files get into gem package.
Thanks in advance!
Related
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?
I am not clear on what certain specifications in the .gemspec file are doing. Specifically,
spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
Can someone explain how these relate to the functionality of a Ruby Gem and why they are necessary?
executables:
Executables included in the gem. For example, the rake gem has rake as an executable. These files must be executable Ruby files.
files:
Files included in the gem. These are the files that will be included in your gem when it is built.
require_paths:
Contains an Array of directories and files which should be added to the $LOAD_PATH on gem activation. By default it is ["lib"].
test_files
Test files included in the gem.
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 :)
There's only one error I get when installing my custom gem: File not found: lib ERROR: While generating documentation
The install continues and says gem installed successfully. However, when I require 'boxes' I get LoadError: no such file to load -- boxes and when I look into the gem directory I notice that there is no lib/ folder in the installed gem. I'm not sure why this is or how to debug it. Any pointers? Below is my gemspec file.
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'boxes/version'
Gem::Specification.new do |gem|
gem.authors = ["Name"]
gem.email = ["http://somedomain.com"]
gem.description = %q{Boxes, a Sass framework}
gem.summary = %q{Boxes is a modular Sass-based framework that is heavily configurable, extremely modular and works as a Sass and a CSS Framework}
gem.homepage = "http://somedomain.com"
gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "boxes"
gem.require_paths = ["lib"]
gem.version = Boxes::VERSION
gem.license = "MIT"
gem.add_dependency "sass", [">= 3.2.0"]
gem.add_development_dependency "rake"
end
I'm new at ruby, and I'm just creating my first gem here, so please bear with me.
One thing I noticed is that on line 1, you're defining "lib" as "../lib", though in gem.require_paths "lib" is in the current directory. Is that true?
Another thing to look for is that lib is not in .gitignore, as you're using git ls-files for defining gem.files
edit
Noticed in your gemfile that you're using ruby 1.8.7, make sure you require 'rubygems' before you require your gem
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