I am in a situation that n a gem foo_bar
that I have written, and which I use as follows:
require 'foo_bar'
include FooBar
suddendly, a constant Baz
appeared:
defined? Baz #=> :constant
Baz.class #=> Module
I forgot where did I define Baz
. If it had any instance methods, I could use #source_location
method to find the file where I defined Baz
. But it has none:
Baz.instance_methods #=> []
How do I find in which file (or where) a module that popped up in my top namespace is defined?
Additional addmission and finding: The constant I am talking about is Net
, and its appearance is not tied to include FooBar
, but to another line, require
of my gem sy
(http://gihub.com/boris-s/sy), which you can install by gem install sy
. By stepping the gem (https://github.com/boris-s/sy/blob/master/lib/sy.rb), I found that none of the require lines, or the top lines up to the module SY
line does not trigger Net
appearance. The module SY
definition does.
Furthermore, I found the pragmatic answer Net
is Net
as in Net::HTTP
. The authors didn't assume that someone would be working with other kinds of nets than internets, and I didn't think about Net::HTTP
, because I was working with Petri nets and defining my own Net
classes. Although I solved my practical problem, it is still interesting to find the general answer of finding modules' sources.
Get the location of a particular module in Python using the OS module. For a pure Python module, we can locate its source by module_name. __file__. This will return the location where the module's .
The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages. You can also use the ActiveState Platform's command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command.
Assuming a file is loaded you can do:
$LOADED_FEATURES.select { |file| File.read(file).include?('module Foo') rescue false }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With