Ruby Require 'File' Doesn't Work But Require './File' Does. Why

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 requireing 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 glance

require './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

In no circumstance will any local
variables in the loaded file be
propagated to the loading environment.

As piyush says a global or constant is fine.

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



Leave a reply



Submit