I recently used the <<- operator to output a multi-line string, like this:
我最近使用<< - 运算符输出多行字符串,如下所示:
<<-form
<h1>Name to say hi!</h1>
<form method="post">
<input type="text" name="name">
<input type="submit" value="send">
</form>
form
But I stole the <<- operator from some Open Source code, but I didn't find any documentation on it.
但我从一些开源代码中偷走了<< - 运算符,但我没有找到任何关于它的文档。
I kinda figured out that it works the same as in bash:
我有点想通知它和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
Does it work that way? I just wanna find documentation on it.
这样做有用吗?我只是想找到它的文档。
5 个解决方案
#1
22
From The Ruby Programming Language:
来自Ruby编程语言:
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.)
对于长字符串文字,可能没有单个字符分隔符可以使用而不必担心记住在文字中转义字符。 Ruby对此问题的解决方案是允许您指定任意字符序列作为字符串的分隔符。这种文字借用了Unix shell语法,在历史上称为here文档。 (因为文档就在源代码中,而不是在外部文件中。)
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:这里的文件以< <或<< - 开头。通过指定结束分隔符的标识符或字符串立即跟踪这些(不允许空格,以防止与左移运算符的歧义)。字符串文字的文本从下一行开始并一直持续到分隔符的文本单独出现在一行上。例如:< p>
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:Ruby解释器通过从输入中一次读取一行来获取字符串文字的内容。然而,这并不意味着< <必须是它自己的最后一件事。实际上,在阅读了这里的文档内容后,ruby解释器会回到它所在的行并继续解析它。例如,以下ruby代码通过连接两个here文档和一个常规的单引号字符串来创建一个字符串:< p>
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.第1行的<< HERE使解释器读取第2行和第3行。<< THERE使解释器读取第4行和第5行。读取这些行后,将三个字符串文字连接成一个。
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""
:这里文档的结尾分隔符实际上必须单独出现在一行上:分隔符后面没有注释。如果here文档以< <开头,那么分隔符必须从行的开头开始。如果文字以<< - 开头,那么分隔符可能在它前面有空格。 here文档开头的换行符不是文字的一部分,但文档末尾的换行符是。因此,每个此处的文档都以行终止符结束,除了此处为空的文档,它与“”相同:< p>
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:如果使用不带引号的标识符作为终止符(如前面的示例中所示),则here文档的行为类似于双引号字符串,用于解释反斜杠转义符和#字符。如果你想要非常非常直观,不允许任何转义字符,请将分隔符放在单引号中。这样做还允许您在分隔符中使用空格:
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:
您还可以使用双引号字符串文字作为here文档的分隔符。这与使用单个标识符相同,只是它允许分隔符内的空格:
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请注意,除了在< <标记之后和文字开头之前的第一行之外,无法在此文档中包含注释。在此代码中的所有#个字符中,一个引入注释,三个插入表达式到文字中,其余的是分隔符< p>
#2
3
http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#here_doc
http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#here_doc
#3
2
This is the Ruby "here document" or heredoc syntax. The addition of the - indicates the indent.
这是Ruby“here document”或heredoc语法。添加 - 表示缩进。
#4
0
The reason why you cannot find any documentation on the <<-
operator is because it isn't an operator. It's literal syntax, like '
or "
.
你无法在<< - 运算符上找到任何文档的原因是因为它不是运算符。它是字面语法,如'或'。
Specifically, it's the here document syntax which is one of the many syntactic forms of string literals in Ruby. Ruby here documents are similar to POSIX sh
here documents, but handling of whitespace removal is different: in POSIX sh
here documents delimited by <<-
, only leading tabs are removed, but they are removed from the contents of the string, whereas in Ruby all leading whitespace is removed, but only from the delimiter.
具体来说,它是这里的文档语法,它是Ruby中字符串文字的众多语法形式之一。这里的Ruby文档类似于POSIX sh这里的文档,但是删除空白的处理是不同的:在POSIX中这里的文档由<< - 分隔,只删除了前导标签,但它们是从字符串的内容中删除的,而在Ruby中删除所有前导空格,但仅从分隔符中删除。
#5
-1
This post will tell you everything you need to know about the "heredoc" string syntax. In addition, you can view the rubydoc page for string syntax.
这篇文章将告诉你关于“heredoc”字符串语法需要知道的一切。此外,您可以查看rubydoc页面以获取字符串语法。
#1
22
From The Ruby Programming Language:
来自Ruby编程语言:
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.)
对于长字符串文字,可能没有单个字符分隔符可以使用而不必担心记住在文字中转义字符。 Ruby对此问题的解决方案是允许您指定任意字符序列作为字符串的分隔符。这种文字借用了Unix shell语法,在历史上称为here文档。 (因为文档就在源代码中,而不是在外部文件中。)
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:这里的文件以< <或<< - 开头。通过指定结束分隔符的标识符或字符串立即跟踪这些(不允许空格,以防止与左移运算符的歧义)。字符串文字的文本从下一行开始并一直持续到分隔符的文本单独出现在一行上。例如:< p>
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:Ruby解释器通过从输入中一次读取一行来获取字符串文字的内容。然而,这并不意味着< <必须是它自己的最后一件事。实际上,在阅读了这里的文档内容后,ruby解释器会回到它所在的行并继续解析它。例如,以下ruby代码通过连接两个here文档和一个常规的单引号字符串来创建一个字符串:< p>
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.第1行的<< HERE使解释器读取第2行和第3行。<< THERE使解释器读取第4行和第5行。读取这些行后,将三个字符串文字连接成一个。
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""
:这里文档的结尾分隔符实际上必须单独出现在一行上:分隔符后面没有注释。如果here文档以< <开头,那么分隔符必须从行的开头开始。如果文字以<< - 开头,那么分隔符可能在它前面有空格。 here文档开头的换行符不是文字的一部分,但文档末尾的换行符是。因此,每个此处的文档都以行终止符结束,除了此处为空的文档,它与“”相同:< p>
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:如果使用不带引号的标识符作为终止符(如前面的示例中所示),则here文档的行为类似于双引号字符串,用于解释反斜杠转义符和#字符。如果你想要非常非常直观,不允许任何转义字符,请将分隔符放在单引号中。这样做还允许您在分隔符中使用空格:
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:
您还可以使用双引号字符串文字作为here文档的分隔符。这与使用单个标识符相同,只是它允许分隔符内的空格:
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请注意,除了在< <标记之后和文字开头之前的第一行之外,无法在此文档中包含注释。在此代码中的所有#个字符中,一个引入注释,三个插入表达式到文字中,其余的是分隔符< p>
#2
3
http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#here_doc
http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.html#here_doc
#3
2
This is the Ruby "here document" or heredoc syntax. The addition of the - indicates the indent.
这是Ruby“here document”或heredoc语法。添加 - 表示缩进。
#4
0
The reason why you cannot find any documentation on the <<-
operator is because it isn't an operator. It's literal syntax, like '
or "
.
你无法在<< - 运算符上找到任何文档的原因是因为它不是运算符。它是字面语法,如'或'。
Specifically, it's the here document syntax which is one of the many syntactic forms of string literals in Ruby. Ruby here documents are similar to POSIX sh
here documents, but handling of whitespace removal is different: in POSIX sh
here documents delimited by <<-
, only leading tabs are removed, but they are removed from the contents of the string, whereas in Ruby all leading whitespace is removed, but only from the delimiter.
具体来说,它是这里的文档语法,它是Ruby中字符串文字的众多语法形式之一。这里的Ruby文档类似于POSIX sh这里的文档,但是删除空白的处理是不同的:在POSIX中这里的文档由<< - 分隔,只删除了前导标签,但它们是从字符串的内容中删除的,而在Ruby中删除所有前导空格,但仅从分隔符中删除。
#5
-1
This post will tell you everything you need to know about the "heredoc" string syntax. In addition, you can view the rubydoc page for string syntax.
这篇文章将告诉你关于“heredoc”字符串语法需要知道的一切。此外,您可以查看rubydoc页面以获取字符串语法。