Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a module's location

Tags:

ruby

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.

like image 879
Boris Stitnicky Avatar asked Mar 07 '14 01:03

Boris Stitnicky


People also ask

How do I find where a Python module is located?

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 .

How do I find libraries in Python?

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.


1 Answers

Assuming a file is loaded you can do:

$LOADED_FEATURES.select { |file| File.read(file).include?('module Foo') rescue false }
like image 84
Jason Waldrip Avatar answered Oct 02 '22 07:10

Jason Waldrip