문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top