#!/usr/bin/env rails runner
/usr/bin/env "rails runner"Which cannot, of course, find the command named 'rails runner' This could be solved by making a wrapper script that calls rails runner on the actual ruby script, but that bothers me, as I'd prefer a single-file solution.
#!/usr/bin/env ruby
exec("/usr/bin/env","rails","runner",File.expand_path($0),*ARGV) unless defined?(Rails)
################################################## # polyglot to use shell to relaunch as 'rails runner' ################################################## if [ "a" != "a" ]; then printf "" # polyglot 'nop' - we are in ruby else # If only we could have this in the #!shebang, but most #! won't allow args set "rbenv" "exec" "rails" "runner" "$0" exec $@ fi end ################################################## # Ruby code below ################################################## puts "Ruby code here"I would love a better solution for the polyglot 'nop' than an empty printf, but it basically works unless you are doing funky things with I/O and can't have any output on stdout. The reason for the 'set' before the 'exec' in the shell code is because it allows for the creation of an array without using ',' - and ruby doesn't see any syntax errors as it does with just "exec a b c..."
#!/usr/bin/awk {system("rails runner " FILENAME); exit}
Back to Solutions.