我正在阅读Ruby的"man"页面,我看到了下面对"-S"旗帜的描述:
-S Makes Ruby use the PATH environment variable to search for script, unless its name begins with a slash. This is used to emulate #! on
machines that don't support it, in the following manner:
#! /usr/local/bin/ruby
# This line makes the next one a comment in Ruby \
exec /usr/local/bin/ruby -S $0 $*
On some systems $0 does not always contain the full pathname, so you need the -S switch to tell Ruby to search for the script if necessary
(to handle embedded spaces and such). A better construct than $* would be ${1+"$@"}, but it does not work if the script is being
interpreted by csh(1).
让我感到困惑的是这一部分:
# This line makes the next one a comment in Ruby \
exec /usr/local/bin/ruby -S $0 $*
这听起来像是man
条目的意思是,\
字符导致它后面的行将被视为注释.
为了测试这一点,我try 创建了一个名为"foo.rb"的新Ruby脚本,其中包括这两行代码和一个简单的"PUTS"语句:
# This line makes the next one a comment in Ruby \
exec /usr/bin/env ruby -S $0 $*
puts "Hi"
我将脚本从/usr/local/bin/ruby
更改为/usr/bin/env ruby
,因为我没有位于/usr/local/bin
的ruby
可执行文件.我认为这不应该以一种有意义的方式影响结果,因为(根据文档)该行应该被视为注释,而不是可执行代码.
如果我对man
条目的解释是正确的,那么当我运行"ruby foo.rb"时,我希望看到Hi
输出到终端.但事实并非如此.相反,我看到了以下几点:
$ ruby foo.rb
foo.rb:2: unknown regexp option - b
exec /usr/bin/env ruby -S $0 $*
foo.rb:2: syntax error, unexpected local variable or method, expecting `do' or '{' or '('
exec /usr/bin/env ruby -S $0 $*
foo.rb:2: syntax error, unexpected global variable, expecting `do' or '{' or '('
exec /usr/bin/env ruby -S $0 $*
我做错了什么?