Error when using require_relative in an executable file - ruby

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.

Related

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.

ruby/rake/rspec cannot load spec_helper (not rails)

I'm trying to set up a testing environment for a ruby project using Rake and Rspec.
When I try to run "rake" in the console I get this error:
C:/Ruby21-x64/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- spec_helper (LoadError)
from C:/Ruby21-x64/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from spec/numbersTest_spec.rb:1:in `<main>'
rake aborted!
My filetree looks like this:
project
-spec
--numbersTest_spec.rb
--spec_helper.rb
-rakefile
rakefile
begin
require 'rspec/core/rake_task'
task default: %w[test]
task :test do
ruby "spec/numbersTest_spec.rb"
end
end
numbersTest_spec.rb
require "spec_helper"
describe "Imperative" do
perfectImperative(5).should == false
end
require_relative 'spec_helper'
instead of require ... in numbersTest_spec.rb will solve this problem.
The reason is that numbersTest_spec.rb has no clue where to look the required file for. require expects an argument to be available on global require path. To solve this in general, one might update $: (global require path), by e.g.:
$:.unshift "#{`pwd`}".chomp
But in your particular case relative requiring is the silver bullet. In fact, bundler was invented to forget about loading path managing horror.

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

ruby `require': cannot load such file

I have a directory tree.
- app.rb
- folder/
-one.rb
app.rb
$:.unshift File.dirname(__FILE__)
require 'folder/one'
When I ran ruby app.rb, I got this error:
`require': cannot load such file -- ./radius/dictionary (LoadError).
I don't know why. Please help.
When the location of the file you are loading is relative to the file you are loading it from, use require_relative:
require_relative 'folder/one'
Try only the below:
$:.unshift(File.dirname(__FILE__) + '/folder')
require 'one'
The above will work.
Your one is not working as,you are adding the directory of the file app.rb as path/to/file.But one.rb is inside the path/to/file/folder/one.rb. So you need to add path/to/file/folder in $:.

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