18 ms·
What I don’t like about backticks in Ruby is that they “ignore” errors in commands you run. It’s up to the program author to remember to check $? for the last e
by jitl 5y ago
What I don’t like about backticks in Ruby is that they “ignore” errors in commands you run. It’s up to the program author to remember to check $? for the last executed command’s exit status. And guess how many times the average Ruby script using this feature implements error handling? Usually it’s totally forgotten.
To be safe, abstractions that make it easy to shell out must also:
- escape all variable interpolations by default using “shellwords” or similar.
- throw exceptions on error by default.
Backticks in Ruby are very easy to use, but aren’t safe.
- eatonphil 5y agoRuby noob, but surely there's a way to metaprogram and change this to default to throw on non-zero exit?
- dvogel 5y agoI agree that people often don't treat the exit code the way they should when using the backticks. However there's better options in the ruby stdlib too, like `system` and `spawn` and `popen`. Using the other methods allows you to also sidestep the shell if you know the arguments you want to pass.
- ilyash 5y agoIn that case, welcome to Next Generation Shell (I'm the author), which actually cares about errors when running external programs. Yes, I mean much more than naive "non-zero is error" because that's just not true in many cases. https://ilya-sher.org/2017/01/28/ngs-unique-features-exit-code-handling/ https://ilya-sher.org/2017/01/28/ngs-unique-features-exit-co...
- hulitu 5y agoI do not want to sound like an oldtimer but even command.com had "if %errorlevel%"