Allow #to_path object as argument to system()
I often work with Pathname objects, but when passing them to a system command I find it a bit tedious that they have to be explicitly converted to a String.
file = BASE + "config.json" system(@cmd, file) #=> TypeError (no implicit conversion of Pathname into String) system(@cmd, file.to_s) #=> works
I propose that the system/exec/spawn family of methods should try to convert their arguments using
to_str fails. I believe it makes perfect sense, since commandline arguments are so often pathnames.
This includes in/out redirection.
system("ls", out: Pathname.new("file")) should be valid.
Updated by zverok (Victor Shepelev) 12 months ago
I always wondered, why
Pathname doesn't define
to_str?.. It seems to be a perfectly suitable option: implicit conversion for "specialized" variety of the
String, and it will make all non-Pathname-aware code work as expected... What are the downsides?
Updated by shevegen (Robert A. Heiler) 12 months ago
I do not have a strong opinion either way; I myself sort of just use
Pathname (I just love
Dir['*.rb'] and things like that), so I would
not need pathname anyway. But I can understand the argument given - 10 years is
also quite a long time ago, and the linked issue actually then refers to a commit
from matz in 2004, so 16 years ago. That's a long time.
I would recommend to focus primarily on
#to_path rather than a
#to_str, if only
for the purpose of the suggestion here; and perhaps suggest it for an upcoming
developer meeting, if this is wanted by Daniel, such as for
Updated by dsisnero (Dominic Sisneros) 11 months ago
I think what would be useful is when interpolating Path like objects (
to_path) that the path objects are shell escaped or cmd escaped when used with
system() or other cmd running objects. There are always errors on windows with directories or files with spaces. I like how Julia language deals with these in shell commands- they automatically escape the strings and expand the arrays but if we can't do that, I do agree with this proposal.
Updated by Eregon (Benoit Daloze) 11 months ago
mame (Yusuke Endoh) wrote in #note-9:
I'd like to confirm:
system(Pathname("cat /etc/passwd"))should attempt to execute a file whose path is
./cat\ /etc/passwd, not attempt to show the content of
Definitely the first, we should take advantage that we know it's supposed to be a path and a not a command line with spaces.
The second would be waiting to become a security vulnerability.