如何判断这里需要哪个文件? [重复]

时间:2022-11-05 13:57:36

This question already has an answer here:

这个问题在这里已有答案:

I'm reading through the codebase of the Homebrew repo, specifically the file here:

我正在阅读Homebrew仓库的代码库,特别是这里的文件:

https://github.com/Homebrew/brew/blob/8518ffdee19c0c985e8631e836b78624e4926c7f/Library/Homebrew/brew.rb

I see many 'require' statements scattered throughout the file, for instance on line 104 (require 'tap'). The problem is that I see 3 files named tap.rb in the codebase:

我看到许多'require'语句散布在整个文件中,例如在第104行(需要'tap')。问题是我在代码库中看到3个名为tap.rb的文件:

Library/Homebrew/tap.rb Library/Homebrew/cmd/tap.rb Library/Homebrew/compat/tap.rb

Library / Homebrew / tap.rb Library / Homebrew / cmd / tap.rb Library / Homebrew / compat / tap.rb

Further down in the code I see Tap.fetch..., and in Library/Homebrew/tap.rb which contains a class named Tap with a class method named fetch, so I'm confident this is the correct file that's being included. But conceivably, there could be dozens of files with the same filename, and more than one of those could have identical class methods. My question is, is there a way to tell which Tap class is being loaded without looking through each of the files?

在代码中我看到Tap.fetch ...,并在Library / Homebrew / tap.rb中包含一个名为Tap的类,其中有一个名为fetch的类方法,所以我确信这是正确的文件。但可以想象,可能存在数十个具有相同文件名的文件,其中不止一个文件具有相同的类方法。我的问题是,有没有办法告诉你加载哪个Tap类而不查看每个文件?

UPDATE: I think I have the answer to my question (see below).

更新:我想我有我的问题的答案(见下文)。

2 个解决方案

#1


0  

If you put a binding.pry before the require and execute $: that will be the list of directories in which the require will look up the 'tap.rb' files.

如果你在require之前放置一个binding.pry并执行$:那将是需要查找'tap.rb'文件的目录列表。

See definition of require: If the filename does not resolve to an absolute path, it will be searched for in the directories listed in $:.

请参阅require的定义:如果文件名未解析为绝对路径,则将在$:中列出的目录中搜索它。

#2


0  

I found this great explanation of how require works in Ruby:

我发现这个有关如何在Ruby中工作的很好的解释:

https://github.com/ericmathison/articles/blob/master/understaning-require-in-ruby.md

Essentially, it works by looking at the Ruby $LOAD_PATH method, similar to how UNIX uses the $PATH variable to look for binary executables when it receives a command in the CLI. I was able to test this out on my local by making two files in the same directory, like so:

本质上,它通过查看Ruby $ LOAD_PATH方法来工作,类似于UNIX在CLI中接收命令时使用$ PATH变量查找二进制可执行文件的方式。我能够通过在同一目录中创建两个文件来测试我的本地,如下所示:

# file number 1- foobar.rb 

module Foobar
  module_function

  def bar
    p "foo" 
  end

end

# file number 2- foobaz.rb

$LOAD_PATH.unshift(".")

class Foobaz

  def baz
    require 'foobar'
    p Foobar.bar
  end

end

Foobaz.new.baz

Without the line $LOAD_PATH.unshift(".") in foobaz.rb, the code wouldn't execute with the simple require 'foobar' statement. I instead had to use require_relative 'foobar.rb'. But adding the current working directory to the $LOAD_PATH environment variable meant that I now had access to all the ruby files in the current dir, so it executed!

如果没有foobaz.rb中的$ LOAD_PATH.unshift(“。”)行,则代码将不会使用简单的require'foobar'语句执行。我不得不使用require_relative'foobar.rb'。但是将当前工作目录添加到$ LOAD_PATH环境变量意味着我现在可以访问当前目录中的所有ruby文件,因此它已执行!

NB- it's likely that adding . to the $LOAD_PATH should go in a config file somewhere, not in the same file as the require statement.

NB-可能会增加。 $ LOAD_PATH应该放在某个配置文件中,而不是与require语句在同一个文件中。

#1


0  

If you put a binding.pry before the require and execute $: that will be the list of directories in which the require will look up the 'tap.rb' files.

如果你在require之前放置一个binding.pry并执行$:那将是需要查找'tap.rb'文件的目录列表。

See definition of require: If the filename does not resolve to an absolute path, it will be searched for in the directories listed in $:.

请参阅require的定义:如果文件名未解析为绝对路径,则将在$:中列出的目录中搜索它。

#2


0  

I found this great explanation of how require works in Ruby:

我发现这个有关如何在Ruby中工作的很好的解释:

https://github.com/ericmathison/articles/blob/master/understaning-require-in-ruby.md

Essentially, it works by looking at the Ruby $LOAD_PATH method, similar to how UNIX uses the $PATH variable to look for binary executables when it receives a command in the CLI. I was able to test this out on my local by making two files in the same directory, like so:

本质上,它通过查看Ruby $ LOAD_PATH方法来工作,类似于UNIX在CLI中接收命令时使用$ PATH变量查找二进制可执行文件的方式。我能够通过在同一目录中创建两个文件来测试我的本地,如下所示:

# file number 1- foobar.rb 

module Foobar
  module_function

  def bar
    p "foo" 
  end

end

# file number 2- foobaz.rb

$LOAD_PATH.unshift(".")

class Foobaz

  def baz
    require 'foobar'
    p Foobar.bar
  end

end

Foobaz.new.baz

Without the line $LOAD_PATH.unshift(".") in foobaz.rb, the code wouldn't execute with the simple require 'foobar' statement. I instead had to use require_relative 'foobar.rb'. But adding the current working directory to the $LOAD_PATH environment variable meant that I now had access to all the ruby files in the current dir, so it executed!

如果没有foobaz.rb中的$ LOAD_PATH.unshift(“。”)行,则代码将不会使用简单的require'foobar'语句执行。我不得不使用require_relative'foobar.rb'。但是将当前工作目录添加到$ LOAD_PATH环境变量意味着我现在可以访问当前目录中的所有ruby文件,因此它已执行!

NB- it's likely that adding . to the $LOAD_PATH should go in a config file somewhere, not in the same file as the require statement.

NB-可能会增加。 $ LOAD_PATH应该放在某个配置文件中,而不是与require语句在同一个文件中。