Ruby“man”文档-使用“\"的多行注解?

pes8fvy9  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(116)

我正在阅读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条目在说\字符导致它后面的行被视为注解。
为了测试这一点,我尝试创建了一个名为“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条目的解释是正确的,那么当我运行“rubyfoo.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 $*

我做错了什么?

flseospp

flseospp1#

看起来Ruby手册页的这一特定部分已经严重过时了。事实上,它最初是在第一次(数据)提交到Ruby的(当时的)SVN存储库时添加的。
当时,Ruby的解析器确实支持在注解末尾使用反斜杠来继续行,如手册页的语法示例所示。
然而,这个特性在1999年3月2日的SVN修订版520中从Ruby中删除了(差异非常大,并且在GitHub中默认不会呈现;检查parse.y文件的第2433行)。
该更改的ChangeLog条目为:

Tue Mar  2 17:04:19 1999  Yukihiro Matsumoto  <[email protected]>

    * parse.y (yylex): backslashes do not concatenate comment lines
      anymore.

1999年8月,Ruby 1.4.0发布了这一变化。从那时起,超过24年前,手册页中的示例不再起作用。
后来(但仍然很旧)的文档suggests a different approach

#!/bin/sh
exec ruby -S -x $0 "$@"
#! ruby
puts 'Hello World'

在这里,exec总是被执行,你仍然使用-S标志来执行Ruby解释器中的当前文件。但是,我们也使用-x标志,它指示Ruby跳过文件中的所有行,直到找到包含#!ruby(或者显然是#! ruby)的行。这在今天仍然有效。

相关问题