ruby gem require_relative cannot load such file - ruby

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

Related

Error when using require_relative in an executable file

This is my executable file, named execute:
#!/usr/bin/env ruby
require_relative '../config/environment.rb'
x = Scraper.new
Item.clear_all
x.create_entire_menu
x.start
environment.rb looks like this:
require 'pry'
require 'nokogiri'
require 'open-uri'
require_relative '../lib/cfaprotein.rb'
require_relative '../lib/item.rb'
require_relative '../lib/scraper.rb'
require_relative '../lib/clifunctions.rb'
environment.rb is in config
execute is in bin
cfaprotein.rb is in lib with item.rb, scraper.rb and clifunctions.rb
cfaprotein.rb has the following at the top:
require "cfaprotein/version"
the other three items in lib have the following at the top:
require_relative './environment.rb'
require 'nokogiri'
require 'open-uri'
require 'pry'
config, lib and bin are all in cfaprotein inside of Development.
When I enter:
ruby bin/execute
I get:
/usr/local/rvm/rubies/ruby-
2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in
`require': cannot load such file -- cfaprotein/version (LoadError)
from /usr/local/rvm/rubies/ruby-
2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in
`require'
from /home/swarbrick85/Development/cfaprotein/lib/cfaprotein.rb:1:in
`<top (required)>'
from /home/swarbrick85/Development/cfaprotein/config/environment.rb:5:in
`require_relative'
from /home/swarbrick85/Development/cfaprotein/config/environment.rb:5:in
`<top (required)>'
from bin/execute:3:in `require_relative'
from bin/execute:3:in `<main>'
What in my code is incorrect and preventing this executable file in bin from executing?
require_relative(string) does the following (emphasis mine):
Ruby tries to load the library named string relative to the
requiring file's path.
Since you're trying to require files from environment.rb using require_relative './foo' it's looking for those files in the same directory as environment.rb (config I believe).
The original question provided a different error message. The new error message points to the same type of problem, which is that when using both require and require_relative you need to understand how the methods work and where your files are located and how to navigate a tree structure.
The link for require_relative is posted but you should also look at require (which is a little more verbose)
Your error message is pretty clear if you weed out the cruft:
cannot load such file -- cfaprotein/version
from /home/swarbrick85/Development/cfaprotein/lib/cfaprotein.rb:1
This would, I believe, expect a version.rb file to live in lib/cfaprotein/version.rb, assuming the lib folder is configured somewhere (maybe a gemspec file, maybe somewhere else) to be in your $LOAD_PATH
If version.rb isn't in the lib/cfaprotein folder, move it there and try it.
If it is in that folder, and still doesn't work, change the require to require_relative './cfaprotein/version.rb' after moving the file.

Ruby `require': cannot load such file (LoadError)

I have a directory structure that looks like this:
- lib
- yp-crawler (directory)
- file-a.rb
- file-b.rb
- file-c.rb
- yp-crawler.rb
My lib/yp-crawler.rb file looks like this:
require "yp-crawler/file-c"
require "yp-crawler/file-b"
require "yp-crawler/file-a"
module YPCrawler
end
When I try to run my file at the command line by doing this:
ruby lib/yp-crawler.rb
I get this error:
`require': cannot load such file -- yp-crawler/file-c (LoadError)
from .rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from lib/yp-crawler.rb:1:in `<main>'
What could be causing this?
According to API Dock, require_relative is what you need.
Ruby tries to load the library named string relative to the requiring
file’s path. If the file’s path cannot be determined a LoadError is
raised. If a file is loaded true is returned and false otherwise.
So all you have to do is
require_relative "file-a"
require_relative "file-b"
require_relative "file-c"
Another thing you can do is to add the directory to $LOAD_PATH (this is how most of the gems require files).
$LOAD_PATH (aka $:) is where ruby looks for a file when you call require.
So you can try this code
# lib/yp-crawler.rb
$LOAD_PATH.unshift File.expand_path('..', __FILE__)
# it can be $LOAD_PATH.push also
require "yp-crawler/file-c"
require "yp-crawler/file-b"
require "yp-crawler/file-a"
module YPCrawler
end
P.S.
For example you can see how paperclip does the same thing.

`require': cannot load such file -- hello (LoadError)

I am attempting to run a test against a hello method contained within the hello file:
ruby hello_spec.rb
which returns:
/usr/local/Cellar/ruby/2.1.2_2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- hello (LoadError)
from /usr/local/Cellar/ruby/2.1.2_2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from hello_spec.rb:116:in `<main>'
The files are contained within the same directory. I've installed RSpec and (I believe) the necessary gems. Other people seem to have similar problems but none of the solutions have worked for me.
I am running Ruby 2.1.2
I am new to Ruby and am struggling (obviously) to get the environment properly configured. Any help is much appreciated.
Note: I didn't write any of the test code. I've literally only made the hello.rb file.
Change require 'hello' to require_relative 'hello' in your hello_spec.rb. Current directory is not included in default ruby load path by default.
Or, alternatively, add the current directory to ruby load path:
$:.unshift File.dirname(__FILE__)
Hope it helps.

Cannot load a custom gem

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.

Why does Ruby find all of my classes except for one named Config?

I have a Ruby program that runs fine on Linux. I'm trying it out on Windows 7 right now, and it should be fine since it only uses two libraries that installed without issues.
The error I'm getting is related to my own code. I have a file called config.rb, which has a class named Config. It has some values that you can change. Sounds pretty harmless.
However, I'm unable to require this class. Ruby gems custom require (i dont use gems at all) is not finding my file. What is going on here?
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- config (LoadError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from apitester.rb:9:in `<main>'
On line 9 of apitester.rb I have:
require 'config'
and config.rb is that simple class, in the same folder.
Try with the following in Ruby 1.8:
require File.join(File.dirname(__FILE__), 'config')
or if you are using in Ruby 1.9:
require_relative 'config'

Resources