Ruby上的<<-运算符,在哪里记录? [英] The <<- operator on Ruby, where is it documented?

查看:107
本文介绍了Ruby上的<<-运算符,在哪里记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我有点想它的作用与bash相同:

I kinda figured out that it works the same as in 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.

推荐答案

来自 Ruby编程语言 :

此处文档

对于长字符串文字,可能没有可以使用的单个字符定界符,而不必担心记住文字中的转义字符. Ruby对这个问题的解决方案是允许您指定任意字符序列作为字符串的定界符.这种文字是从Unix Shell语法中借用的,历史上称为 here文档. (因为该文档位于源代码中,而不是外部文件中.)

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

Ruby解释器通过一次从其输入中读取一行来获取字符串文字的内容.但是,这并不意味着<<必须是最后一行.实际上,在阅读了here文档的内容之后,Ruby解释器将返回到原来所在的行,并继续对其进行解析.例如,以下Ruby代码通过连接两个此处文档和一个常规的单引号字符串来创建一个字符串:

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

第1行上的<<HERE使解释器读取第2和3行.<<THERE使解释器读取第4和5行.在读取了这些行之后,将三个字符串文字串联在一起.

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.

here文档的结尾定界符确实必须单独出现在一行上:在定界符后不能加任何注释.如果here文档以<<开头,则定界符必须从该行的开头开始.如果文字以<<-开头,则分隔符前面可能带有空格. here文档开头的换行不是文字的一部分,而文档末尾的换行则是该文字的一部分.因此,每个此处的文档都以行终止符结尾,但空的此处文档与"":

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上的&lt;&lt;-运算符,在哪里记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆