Ruby同目录文件require问题求助:为何无法直接使用require 'filename'?
Hey there, let’s clear up this confusion once and for all—Ruby’s file handling rules can feel counterintuitive at first, but your core understanding is totally correct! Let’s break it down step by step:
Why require 'filename' doesn’t work for same-directory files
Ruby does not automatically add the directory of your current script to its load path (the $LOAD_PATH array, also shorthanded as $:). This is true no matter where you run the script from—even if you’re in the exact directory the script lives in.
Rails works around this because the framework explicitly modifies $LOAD_PATH during boot, adding directories like app/models, app/controllers, and lib so you can require files without fuss. But plain Ruby scripts don’t get that magic out of the box.
The catch with require './filename'
Using ./filename works only when you run the script from its own directory because this path is relative to your current working directory (the directory you’re in when you type the ruby command), not the directory where the script is stored.
For example:
- If you’re in
~/my-projectand runruby script.rb,./filenamelooks in~/my-project. - But if you’re in
~and runruby my-project/script.rb,./filenamestill looks in~—not~/my-project—so it fails to find the file.
This same problem applies to File.open("./file.txt"): the path is relative to your working directory, not the script’s directory. That’s why even if all your files are in the same folder, running the script from a different location breaks file access.
How to fix this (the right way)
The simplest, most reliable solution is to use paths relative to the script’s own directory instead of the working directory. Here’s how:
1. Loading other Ruby files
Use require_relative—it’s designed specifically to load files relative to the current script’s location, regardless of where you run the script from:
# Loads filename.rb from the same directory as this script require_relative 'filename'
If you prefer to use require, you can build an absolute path with __dir__ (a Ruby method that returns the absolute path of the current script’s directory):
require File.join(__dir__, 'filename')
2. Opening other files (like text files)
Again, use __dir__ to build an absolute path to your file:
# Opens file.txt from the same directory as this script, no matter where you run it File.open(File.join(__dir__, 'file.txt')).readlines.each do |line| # Process each line here end
Bonus: Adding the script directory to $LOAD_PATH (not recommended for beginners)
If you really want to use require 'filename' without paths (and you’re okay with the minor security risks), you can add the script’s directory to $LOAD_PATH at the top of your script:
$LOAD_PATH.unshift(__dir__) # Now you can do this: require 'filename'
But this can lead to confusion later if you have files with the same name in different directories, so it’s better to stick with require_relative for most cases.
To confirm your original thought
You were exactly right: Ruby’s interpreter does not default to looking in the script’s directory for unqualified file names. It relies entirely on the $LOAD_PATH array for require calls, and uses your working directory for relative paths like ./file.txt.
内容的提问来源于stack exchange,提问作者Just Matt for now

