All writing

Getting the root directory of your gem

June 3, 2011 1 min read Luke Chadwick

This post is 15 years old. It was originally published on vertis.io; links and advice may have aged.

I've been writing a few gems recently. One thing that you inevitably need to be able to do is find the root directory of your gem.

Assuming that you place this in 'lib/your_gem.rb', the following code will return the path to the root of your gem:

module YourGem
  def self.root
    File.expand_path('../..',__FILE__)
  end
end