Ruby“man”文档-使用“\”进行多行注释?

Ric*_*mas 2 ruby

我正在阅读 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).
Run Code Online (Sandbox Code Playgroud)

我感到困惑的部分是这部分:

# This line makes the next one a comment in Ruby \
exec /usr/local/bin/ruby -S $0 $*
Run Code Online (Sandbox Code Playgroud)

这听起来像是man条目在说该\字符导致其后面的行被视为注释。

为了测试这一点,我尝试制作一个名为“foo.rb”的新 Ruby 脚本,其中包含这两行和一个简单的“puts”语句:

# This line makes the next one a comment in Ruby \
exec /usr/bin/env ruby -S $0 $*

puts "Hi"
Run Code Online (Sandbox Code Playgroud)

我将脚本从 更改/usr/local/bin/ruby为 ,/usr/bin/env ruby因为我没有ruby位于 的可执行文件/usr/local/bin。我认为这不会以有意义的方式影响结果,因为(根据文档)该行应该被视为注释,而不是可执行代码。

如果我对条目的解释man是正确的,我希望看到Hi在运行“ruby foo.rb”时看到终端的输出。但事实并非如此。相反,我看到以下内容:

# This line makes the next one a comment in Ruby \
exec /usr/local/bin/ruby -S $0 $*
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Hol*_*ust 5

Ruby 手册页的这个特定部分似乎已经严重过时了。事实上,它最初是在第一次(数据)提交到 Ruby(当时的)SVN 存储库时添加的。

当时,显然 Ruby 的解析器确实支持在注释末尾带有反斜杠的行延续,如手册页的语法示例所示。

然而,该功能已于 1999 年 3 月 2 日在SVN 修订版 520中从 Ruby 中删除(差异相当大,并且默认情况下不会在 GitHub 中呈现;检查文件的第 2433 行parse.y)。

该更改的ChangeLog 条目如下:

Tue Mar  2 17:04:19 1999  Yukihiro Matsumoto  <matz@netlab.co.jp>

    * parse.y (yylex): backslashes do not concatenate comment lines
      anymore.
Run Code Online (Sandbox Code Playgroud)

此更改于 1999 年 8 月随 Ruby 1.4.0 一起发布。从那时起,距今已有 24 年多了,手册页中的示例不再起作用。

后来(但仍然很旧)的文档提出了一种不同的方法

Tue Mar  2 17:04:19 1999  Yukihiro Matsumoto  <matz@netlab.co.jp>

    * parse.y (yylex): backslashes do not concatenate comment lines
      anymore.
Run Code Online (Sandbox Code Playgroud)

在这里,exec始终会执行,并且您仍然使用该-S标志在 Ruby 解释器中执行当前文件。然而,我们还使用-x指示 Ruby 跳过文件中所有行的标志,直到找到包含#!ruby(或显然#! ruby)的行。这在今天仍然有效。