且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么相对路径在 Ruby 中不起作用需要

更新时间:2023-02-09 23:18:01

相对路径基于工作目录.我假设在同一目录中有主文件.如果您在项目根目录上运行 ruby ./shapes/main.rb,ruby 会尝试找到 {project_root}/shape.rb,而不是 {project_root}/shapes/shape.rb.它不起作用.

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.

你需要像下面这样使用 require_relative.

You need to use require_relative like below.

# {project_root}/shapes/main.rb
require_relative './shape'
require_relative './rectangle'
require_relative './square'