什么是'<<~' called in Ruby, how does it work?

问题描述 投票:0回答:1

示例:

sentence = <<~ LOTSOFWORDS
this is an example of the thing initialising a variable, how else does this work?
LOTSOFWORDS

我不认为这是铲子操作员,里面没有波浪号。我正在做一个 Kata,用它形成字符串变量,所以我知道这么多。

我尝试在 Google 上查找“<<~', 'less than less than tilde' and a few other guesses at what it's called. Any clues so I can work it out?

ruby
1个回答
0
投票

这被称为

heredoc
,更具体地说是“波浪形”定界符。您可以在此处查看相关文档。

它被用作将文本块定义为多行字符串的优雅方式,而不会搞乱页面上的缩进。

Heredocs 自诞生以来就几乎一直使用 Ruby 语言。 “squiggly”heredoc 语法(将左侧的空格去掉,这意味着有时可以更优雅地缩进代码)已添加到 2015 年发布的 ruby 2.3 版本中。

© www.soinside.com 2019 - 2024. All rights reserved.