Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find file path of a given class

I have a reference to the Class object of a given class. I need to find out the file path of the class. The class inherits from ActiveRecord if that is any help.

Any easy way of doing this?

Thanks.

like image 267
Finbarr Avatar asked May 01 '12 23:05

Finbarr


People also ask

How do you find the path of a class?

We can use the getClass() method of Object class in Java that returns the currently running class, which further can be used with the getPath() method to get the path of the current working directory. See the example below.

How do I find a specific file path?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I find the path of a file in Java?

In Java, for NIO Path, we can use path. toAbsolutePath() to get the file path; For legacy IO File, we can use file. getAbsolutePath() to get the file path.

How do I get the file path in Python?

To retrieve a file in Python, you need to know the exact path to reach the file, in Windows, you can view a particular file's path by right-clicking the File-> Properties-> General-> Location. Similarly, to run a script, the working directory needs to be set to the directory containing the script.


1 Answers

Use source_location on its methods:

YourClass.instance_methods(false).map { |m| 
  YourClass.instance_method(m).source_location.first
}.uniq

You might get more than one location, as methods might be defined in different places.

like image 148
Marc-André Lafortune Avatar answered Oct 09 '22 22:10

Marc-André Lafortune