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 :)
Related
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.
I have a real newbie problem. I wrote a very small (one-file) library and I wanted to publish it in a gem so that I can use it in other projects modularly. I used the following gemspec:
Gem::Specification.new do |s|
s.name = 'symbolize-array'
s.version = '1.0.0'
s.date = '2013-11-22'
s.summary = "Symbolizes strings in arrays"
s.description = ""
s.files = ["lib/array.rb"]
s.homepage =
'https://github.com/renra/symbolize-array-ruby'
s.license = 'MIT'
end
I build the gem. Fine. I publish the gem. Fine. I install the gem from rubygems. Fine. But when I run irb and do require 'symbolize-array' I get:
LoadError: cannot load such file -- symbolize-array
from /home/renra/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /home/renra/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from (irb):4
from /home/renra/.rvm/rubies/ruby-2.0.0-p247/bin/irb:13:in `<main>'
As you can see from the backtrace I use rvm. When I run 'gem environment' and I go to the gem path I can see my gem is installed just like the others. I can require the others, but I cannot require my gem. So I guess this is not a problem with the load path (I've seen questions that were answered like that) but maybe in the way I built the gem. Grateful for your ideas.
You would need to require "array" to load your library, which may not work at all due to name clashes with other gems. When you install a gem, it just adds its library folder to the search path, and requires find files by name (without the .rb), they don't reference gem names.
Take a look at the naming conventions for gem components and also the link http://guides.rubygems.org/name-your-gem/
I suspect you should name the file "lib/symbolize_array.rb" and probably the gem name should be "symbolize_array" - ideally it would also implement a class or module SymbolizeArray in that case, but that is less important.
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
I'm trying to build my first ruby gem that contains some classes I want to use across projects - for the sake of this question it is called test_service_api.
I have written and built the gem using "gem build test_service_api.spec". I have then done a "gem install test_service_api-0.0.2.gem".
The spec file is:
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/test_service_api/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Tyler Power"]
gem.email = ["tyler#xxx.com"]
gem.description = %q{}
gem.summary = %q{}
gem.homepage = ""
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 = "test_service_api"
gem.require_paths = ["lib"]
gem.version = TestServiceApi::VERSION
end
I'm now trying to use the gem in a little test project, in the test projects Gemfile I have specified a dependency on the gem I built:
source 'https://rubygems.org'
gem 'sinatra'
gem 'test_service_api'
I can then do a "bundle install" and the bundler correctly finds all my dependencies and puts them in vendor/cache:
Tylers-MacBook-Pro:Test tyler$ bundle install
Using rack (1.4.1)
Using rack-protection (1.2.0)
Using test_service_api (0.0.2)
Using tilt (1.3.3)
Using sinatra (1.3.2)
Using bundler (1.1.3)
Updating .gem files in vendor/cache
Your bundle is complete! It was installed into ./vendor/bundle
So that all looks good to me so far, the issue I have is that when I try to require the gem in a source file in the test project it can't find it:
require 'rubygems'
require 'sinatra'
require 'test_service_api'
get '/' do
res = ''
ENV.each do |k, v|
res << "#{k}: #{v}<br/>"
end
res
end
My IDE complains of "No file to load" and if I try and run the app I get:
./app.rb:3:in `require': no such file to load -- test_service_api (LoadError)
from ./app.rb:3
I'm close to pulling my hair out... Does anyone have any ideas on how to resolve this?
What about trying this:
require "rubygems"
require "bundler/setup"
instead of just rubygems?