We have a helper for this, see: https://github.com/Homebrew/homebrew/blob/master/Library/Homebrew/extend/pathname.rb#L242