Question

What's the simple equivalent of a Bash

exec $0 $*

in Ruby?

http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-exec

EDIT

Part of the answer seems to be:

  exec( "/bin/ls", *ARGV)  # credits: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/13379

So next, will the variable $0 hold current script's complete name and path, or Ruby binary? Under irb only "irb" returns, implying I'd need to find and prepend the path, at least.

Another solution on the fringes comes from https://stackoverflow.com/a/2966988/1069375

cmd_line = "#{$0} #{ARGV.join( ' ' )}" 

However, trying exec cmd_line seems subject to shell expansion, if it can separate the command from its parameters. The ARGV.join itself would cram all the arguments together as one.

Was it helpful?

Solution

This should execute the current file without invoking a subshell:

exec([ File.absolute_path(__FILE__), __FILE__ ], *ARGV)

EDIT: Of course, it breaks if you're changing the current working directory in your script. That is easy to work around, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top