Ruby: How to determine the absolute path relative to a file

Updated . Posted . Visible to the public. Repeats.

If you want to get the path of a file relative to another, you can use the expand_path method with either the constant __FILE__ or the method __dir__. Read this card for more information about __FILE__ and __dir__.



├── bin
│   ├── format_changelog


#!/usr/bin/env ruby

changelog_path = ? # How to get the path to ../ independent of the working dir of the caller
changelog =

# ... further actions here

Ruby >= 2.0

changelog_path = File.expand_path('../', __dir__)

Ruby < 2.0

changelog_path = File.expand_path('../../', __FILE__)


changelog_path = Rails.root.join('')
Last edit
Felix Eschey
Source code in this card is licensed under the MIT License.
Posted by Emanuel to makandra dev (2020-09-18 06:39)