In Swift, abbreviatingWithTildeInPath
creates “a new string that replaces the current home directory portion of the current path with a tilde (~) character”. In other words, it turns /Users/username/Desktop/
into ~/Desktop
.
In Ruby we can do the reverse with File.expand_path
:
File.expand_path('~/Desktop')
# => /Users/username/Desktop/
But there’s seemingly no native way to abbreviate a path with a tilde. I’m achieving it with:
'/Users/username/Desktop/'.sub(/^#{ENV['HOME']}/, '~')
That seems to work reliably, but are there any flaws I’m missing? Or better yet, is there a native method?
There are several subtle issues with your approach
Dir.home
is a more reliable way to get the home directory in case the script is running a weird environment.
, which might cause false positives in a regex. Fixed with Regexp.escape
^
and $
will find the first line that matches, not just the start of the string. Fixed with \A
and \Z
, though realistically you should never be passing multiline strings to this methoddef File.collapse_path path
path.sub /\A#{Regexp.escape Dir.home}(?=\Z|#{File::SEPARATOR})/, ?~
end