[ruby-core:113925] [Ruby master Feature#19734] Let `File` be a subclass of `Dir`

Issue #19734 has been reported by sawa (Tsuyoshi Sawada). ---------------------------------------- Feature #19734: Let `File` be a subclass of `Dir` https://bugs.ruby-lang.org/issues/19734 * Author: sawa (Tsuyoshi Sawada) * Status: Open * Priority: Normal ---------------------------------------- I propose to let `File` class be a subclass of `Dir` class. Motivations are as follows. When we want to do something with a file (for example `File.read` or `File.write`), we quite often want to achieve the home directory. If we are in the mode of thinking in the ream of `File`, we would likely come up with `File.expand_path("~")`. However, this is cumbersome, and actually, `Dir.home` is simpler. But my experience is that switching the mind to `Dir` when thinking about `File` require extra cognitive load. Also, many methods defined in `File` work for both a file or a directory. When we are doing something with a directory, we have to stop and think whether the method we want to use is defined in `File` or in `Dir`. It is possible to do that, but that also requires extra cognitive load. I thought things would become simpler if we do not need to switch between these two classes in mind, and keep using `File`. There are a few singleton methods that are defined on both `File` and `Dir`: ``` delete/unlink, empty?, exist?, new/open ``` but I think there would be no problem in choosing the appropriate class depending on the need. Semantically, maybe it makes more sense to let `Dir` be a subclass of `File` because directories are special cases of files. However, in Ruby, `File` and `Dir` have ancestors ``` File < IO < Enumerable < File::Constants < Object < ... Dir < Enumerable < Object < ... ``` respectively, and `File` inherits `IO` and `File::Constants`, beside their common ancestors, so I though that letting `File` be a subclass of `Dir` would be more practical. -- https://bugs.ruby-lang.org/

Issue #19734 has been updated by matz (Yukihiro Matsumoto). Status changed from Open to Rejected I am negative for several reasons. * IO (File) and Dir have different role and implementation in Unix, which Ruby design based on * In Ruby, we cannot have multiple class parents, case for your proposal, File's super class must be either IO *or* Dir * Last but not least, according to your proposal, we will have a “File is a Dir” relationship, which is unacceptably strange (at least from my point of view). To reduce (your) cognitive load, we might be able to add some (class) methods, without making a new inheritance relationship. Matz. ---------------------------------------- Feature #19734: Let `Dir` methods be available to `File` https://bugs.ruby-lang.org/issues/19734#change-103584 * Author: sawa (Tsuyoshi Sawada) * Status: Rejected * Priority: Normal ---------------------------------------- I propose to let `Dir` singleton methods be available to `File` in some way. Motivations are as follows. When we want to do something with a file (for example `File.read` or `File.write`), we quite often want to achieve the home directory. If we are in the mode of thinking in the ream of `File`, we would likely come up with `File.expand_path("~")`. However, this is cumbersome, and actually, `Dir.home` is simpler. But my experience is that switching the mind to `Dir` when thinking about `File` require extra cognitive load. Also, many methods defined in `File` work for both a file or a directory. When we are doing something with a directory, we have to stop and think whether the method we want to use is defined in `File` or in `Dir`. It is possible to do that, but that also requires extra cognitive load. I thought things would become simpler if we do not need to switch between these two classes in mind, and keep using `File`. There are a few singleton methods that are defined on both `File` and `Dir`: ``` delete/unlink, empty?, exist?, new/open ``` For them, perhaps some modification can be made. -- https://bugs.ruby-lang.org/
participants (2)
-
matz (Yukihiro Matsumoto)
-
sawa (Tsuyoshi Sawada)