Ruby require 'file' doesn't work but require './file' does. Why?
If you want to require
a file not from the system $LOAD_PATH
but rather relative to the directory of the file you are require
ing from, you should use require_relative
. (Which, as you can see, isn't exactly extensively documented.)
Require Not Working in Ruby
Current directory in NOT on the ruby search path by default. Add it to $:
and everything will be fine (assuming you are launching irb
from the directory where person.rb
is located):
$: << "."
require "person.rb"
More detailed info. Cannot require ruby files from a directory other then the one with main files (LoadError)
require
searches in your $LOAD_PATH
. require
should generally be used if you want to use a gem that is installed on your system. For your purposes you want to use require_relative
:
require_relative 'riverbattle/version'
Note that at first glancerequire './riverbattle/version'
might also seem to work. However, .
here points to the current directory from where you run the process, not the directory where the file resides, obviously this is not what you want. Ruby 'require' error: cannot load such file
I just tried and it works with require "./tokenizer"
.
what is the possible reason for require a file but it did not run
The docs for Kernel#load says
As piyush says a global or constant is fine.In no circumstance will any local
variables in the loaded file be
propagated to the loading environment.
load works on local path, require doesn't
If you provide just a filename to require
, it will only look in the predefined $LOAD_PATH
directories. However, if you provide a path with your filename, it should work:
puts 'This is the first (master) program file.'
require './loadee.rb'
puts 'And back again to the first file.'
You could also add your project's folder to the load path instead:$LOAD_PATH.unshift File.dirname(__FILE__)
puts 'This is the first (master) program file.'
require 'loadee.rb'
puts 'And back again to the first file.'
And last, you could just use require_relative
instead:puts 'This is the first (master) program file.'
require_relative 'loadee.rb'
puts 'And back again to the first file.'
Why relative path doesn't work in Ruby require
Relative path is based on working dir. I assume that there is main file on the same directory. If you run ruby ./shapes/main.rb
on project root, ruby try to find {project_root}/shape.rb
, not {project_root}/shapes/shape.rb
. It doesn't work.
You need to use require_relative
like below.
# {project_root}/shapes/main.rb
require_relative './shape'
require_relative './rectangle'
require_relative './square'
Why do some Ruby projects have a file with just 'require' statements
It's just a wrapper that calls other requirements so one user script would not be require-ing all dependencies. Sometimes a package would prefer dividing its functions on different files than placing all of its code on a single file. It's also easier to make changes and track errors. Committing changes to a repository could also be lighter.
Ruby 'require' error: cannot load such file
I just tried and it works with require "./tokenizer"
.
Related Topics
Ruby on Rails Query with Sum and Group
How to Use Reform to Prepopulate for Featured Objects
Streaming CSV Download from Rails 3.2 App
After_Save Callback to Set The Updated_By Column to The Current_User
Rvm - Macports Won't Update Through Proxy
How to Test If a Value Is a Prime Number in Ruby? Both The Easy and The Hard Way
Rvm Can No Longer Install 1.8.7-P352 on MAC Os X Mountain Lion
Prawn Doesn't Seem to Push Layout Down When Using Repeat(:All)
Getting a "Connection Reset by Peer" Error When Hitting Google Contacts API
Warning While Installing The Rails Plugin
Define_Method with Predefined Keyword Arguments
Heroku Process Exits with Status 0 But Its Dyno Still "Crashes"
What Is 'stringify_Keys' in Rails and How to Solve It When This Error Comes
Crontab Not Running Ruby Script
How to Stub/Mock a Call to The Command Line with Rspec
How to Validate This Hash Parameter
Parsing a CSV File Using Different Encodings and Libraries
Why The Unit Test Frameworks in Fortran Rely on Ruby Instead of Fortran Itself