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 $:.
Related
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.
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
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.
I am trying to require:
PROJECT_DIR/lib/shrt.rb
from:
PROJECT_DIR/test/shrt_test.rb
I tried:
require File.expand_path('.././lib/shrt.rb')
# and
require './lib/shrt.rb'
# and
require_relative '../lib/shrt.rb'
However it gives me an error stating it cannot load a file that is required in the shrt.rb file in lib/. In PROJECT_DIR/, I ran via Rake a Ruby test file:
ruby 'test/shrt_test.rb'
Any idea what I'm doing incorrectly?
Does:
require './lib/shrt.rb'
Not work? I've always required files in projects that way.
I needed to require the files in the following way:
require './lib/shrt/version'
require './lib/shrt/client'
...in the file I was trying to require in the test file. I required that file using require_relative '../lib/shrt'.
EDIT However now, when I rake install the gem, I'm using Bundler, and run it, I get `require': cannot load such file -- ./lib/shrt/version (LoadError).
EDIT 2 Fixed the problem by require_relative instead of just require on the version file.
I have created a simple app and this is my folder structure
lib/book.rb
lib/user.rb
server.rb <- main sinatra file
And this is my config.ru
require './server'
run Sinatra::Application
When I deploy to heroku I got this error `require': no such file to load -- lib/book (LoadError). However, on my local machine it works fine.
I'm not sure what to include in config.ru I tried require './lib/book' as well, but it didn't work.
Thanks a lot.
You could try to add the /lib directory to your $LOAD_PATH by adding something like this to your server.rb:
configure do
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
Dir.glob("#{File.dirname(__FILE__)}/lib/*.rb") { |lib|
require File.basename(lib, '.*')
}
end
This will add the /lib directory to your $LOAD_PATH and require all *.rb files in it.