Intro
Did you know that recent versions of gem have an npx-style way of invoking a gem's script of the same name? So today, this already works 🎉
#!/usr/bin/env gem exec rubyshell exec
sh do
puts date
puts pwd
end
# $ chmod +x my_script
# $ ./my_script
Sun Jun 7 10:44:22 PDT 2026
/Users/john/tmp
Idea 1
You can add that to the docs
Idea 2
a "no-sh-block" mode where the entire file is assumed to be rubyshell, no sh do needed. Could implement this with a flag (rubyshell exec --top-level) or simply autodetect when the script runs (check for presence of sh do in the script, either with a simple grep or string search, or maybe fancier way with prism?).
Plot-twist: I lied a little
the actual shebang I used is: #!/usr/bin/env -S RUBYOPT=-W0 gem exec --silent rubyshell exec
RUBYOPT=-W0 and --silent are needed to keep the output nice
maybe that approach is fine or maybe the gem exec feature could be refined, I'm not sure what folks would consider safe as default behavior.
Intro
Did you know that recent versions of
gemhave annpx-style way of invoking a gem's script of the same name? So today, this already works 🎉Idea 1
You can add that to the docs
Idea 2
a "no-sh-block" mode where the entire file is assumed to be rubyshell, no
sh doneeded. Could implement this with a flag (rubyshell exec --top-level) or simply autodetect when the script runs (check for presence ofsh doin the script, either with a simple grep or string search, or maybe fancier way with prism?).Plot-twist: I lied a little
the actual shebang I used is:
#!/usr/bin/env -S RUBYOPT=-W0 gem exec --silent rubyshell execRUBYOPT=-W0and--silentare needed to keep the output nicemaybe that approach is fine or maybe the
gem execfeature could be refined, I'm not sure what folks would consider safe as default behavior.