ruby - Ruby 上的 <<- 运算符,它在哪里记录?

标签 ruby operators

我最近使用 <<- 运算符输出多行字符串,如下所示:

<<-form
  <h1>Name to say hi!</h1>
  <form method="post">
    <input type="text" name="name">
    <input type="submit" value="send">
  </form>
form

但是我从一些开源代码中窃取了 <<- 运算符,但我没有找到任何关于它的文档。

我有点发现它和 bash 中的工作方式一样:

$ cat <<EOF >> form.html
> <h1>Name to say hi!</h1>
> <form method="post">
>   <input type="text" name="name">
>   <input type="submit" value="send">
> </form>
> EOF

是这样的吗?我只想找到有关它的文档。

最佳答案

来自 The Ruby Programming Language :

Here Documents

For long string literals, there may be no single character delimiter that can be used without worrying about remembering to escape characters within the literal. Ruby's solution to this problem is to allow you to specify an arbitrary sequence of characters to serve as the delimiter for the string. This kind of literal is borrowed from Unix shell syntax and is historically known as a here document. (Because the document is right here in the source code rather than in an external file.)

Here documents begin with << or <<-. These are followed immediately (no space is allowed, to prevent ambiguity with the left-shift operator) by an identifier or string that specifies the ending delimiter. The text of the string literal begins on the next line and continues until the text of the delimiter appears on a line by itself. For example:

document = <<HERE        # This is how we begin a here document
This is a string literal.
It has two lines and abruptly ends...
HERE

The Ruby interpreter gets the contents of a string literal by reading a line at a time from its input. This does not mean, however, that the << must be the last thing on its own line. In fact, after reading the content of a here document, the Ruby interpreter goes back to the line it was on and continues parsing it. The following Ruby code, for example, creates a string by concatenating two here documents and a regular single-quoted string:

greeting = <<HERE + <<THERE + "World"
Hello
HERE
There
THERE

The <<HERE on line 1 causes the interpreter to read lines 2 and 3. And the <<THERE causes the interpreter to read lines 4 and 5. After these lines have been read, the three string literals are concatenated into one.

The ending delimiter of a here document really must appear on a line by itself: no comment may follow the delimiter. If the here document begins with <<, then the delimiter must start at the beginning of the line. If the literal begins with <<- instead, then the delimiter may have whitespace in front of it. The newline at the beginning of a here document is not part of the literal, but the newline at the end of the document is. Therefore, every here document ends with a line terminator, except for an empty here document, which is the same as "":

empty = <<END
END

If you use an unquoted identifier as the terminator, as in the previous examples, then the here document behaves like a double-quoted string for the purposes of interpreting backslash escapes and the # character. If you want to be very, very literal, allowing no escape characters whatsoever, place the delimiter in single quotes. Doing this also allows you to use spaces in your delimiter:

document = <<'THIS IS THE END, MY ONLY FRIEND, THE END'
    .
    . lots and lots of text goes here
    . with no escaping at all.
    .
THIS IS THE END, MY ONLY FRIEND, THE END

The single quotes around the delimiter hint that this string literal is like a single-quoted string. In fact, this kind of here document is even stricter. Because the single quote is not a delimiter, there is never a need to escape a single quote with a backslash. And because the backslash is never needed as an escape character, there is never a need to escape the backslash itself. In this kind of here document, therefore, backslashes are simply part of the string literal.

You may also use a double-quoted string literal as the delimiter for a here document. This is the same as using a single identifier, except that it allows spaces within the delimiter:

document = <<-"# # #"    # This is the only place we can put a comment
<html><head><title>#{title}</title></head>
<body>
<h1>#{title}</h1>
#{body}
</body>
</html>
               # # #

Note that there is no way to include a comment within a here document except on the first line after the << token and before the start of the literal. Of all the # characters in this code, one introduces a comment, three interpolate expressions into the literal, and the rest are the delimiter

关于ruby - Ruby 上的 <<- 运算符,它在哪里记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1644132/

相关文章:

ruby - 编码默认为 ASCII-8BIT

arrays - -join ";"是如何工作的?

ruby-on-rails - 如何创建 seeds.rb 数组?

ruby - 如何使用将文件发送到服务器的 HTTParty (Net::HTTP) 发送 POST 请求

javascript - 我在解释 Javascript 运算符的不等式时遇到一些问题

algorithm - 运算符谜语

c++ - c++ '+'算子使用 '+='算子统一实现

c++ - catch 在此 C++ 代码中的用法是什么

ruby-on-rails - Ruby 有 natural_sort_by 方法吗?

Ruby:如何以最简单的方式并行运行任务