pathname
pathname copied to clipboard
Request to add Dir#chdir to Pathname functionality.
Since most use-full functionalities from Dir are included in Pathname, I'm surprised that Dir.chdir is not included. As the whole Pathname class is as pleasant facade for File, FileTest, Dir and FileUtils, and working with it should be 'unsurprising', I think the chdir command should also be a part of the Pathname class/
@rempargo Can you explain detailed usecase of Pathname#chdir? Inconsistency reason is weak to add Pathname#chdir again since Ruby 1.9.
One thing I don't feel quite right is that calling the chdir method on a relative Pathname would make the receiver no longer meaningful.
@hsbt My thinking in this case (that led to the PR) is that:
- In general, I feel that consistency is not that weak reason. For me,
Pathnameis an OO wrapper around the “filesystem object,” which allows to do all (or a huge part) of whatFile/Dir/FileUtilsmodule functions allow to do without leaving the comfort of OO wrapper. Like, you switch toPathnamefor pathing around references to filesystem objects, and then everything you might want to do with filesystem (read/write/rename/get and set properties) can be done withpath.some_method. So, just some methods not existing inPathnamemight lead to user confusion (and even avoiding the OO wrapper because “I don’t remember when it might turn out it doesn’t have the method I expected to be there”). Especially after instance method was introduced toDir, it became more confusing (as “simpler”Dirobject now can do things “more advanced”Pathnameobject can’t). - Say,
Rails.rootis aPathname, and it is usually advised (and even checked by some linters) that people wouldn’t doFile.read("{Rails.root}/my/config.yml"), but do insteadRails.root.join('my', 'config.yml').read. Conversely, say, some application maintanance tasks might want to dochdirto some subdirectory of the app (to run some scripts there), and thereforeRails.root.join('maint', 'scripts').chdir { exec('something') }is not unimaginable, at least! - I understand the concern about the relative
Pathnamebecoming “irrelevant” afterchdirto it (unlikeDir#chdir, asDirobject is bound to FS object early), but I don’t think it is too confusing, considering the nature of thePathname. It is more or less the same as that afterpath.deleteyou can’t anymore dopath.read: you performed some operation, value stayed the same, referred object changed, at least it is easy to explain.