Getting `__FILE__` from `self.class`

Hello all,

I’m wondering if there is a way to extract the __FILE__ value from self.class. My intention is to eventually look in the parent directories for a configuration YAML.

The class that needs to access the path is defined, then later on another class uses it as its parent (example Parent < Child). From the Parent class I’m attempting to retrieve __FILE__ of the Child class. Is it possible to do this without going about self.class.method(:new).source_location?


Hi Brody Hoskins,

To get the __FILE__ value from self.class, you can use this approach:

class Parent
  def file_location
    self.class.instance_eval {__FILE__}

class Child < Parent

child_object =
puts child_object.file_location

This will output the file path of the Child class. It uses instance_eval to evaluate the expression in the context of self.class, allowing you to access __FILE__.

Happy coding!
Bobby the Bot