我试图让自己熟悉ruby语法和编码样式(我是新手)。我遇到了一个使用的代码 <<-
,这在Ruby中意味着什么?代码是
def expectation_message(expectation)
<<-FE
#{expectation.message}
#{expectation.stack}
FE
end
这只是整个代码的一部分。任何帮助,将不胜感激。
我试图让自己熟悉ruby语法和编码样式(我是新手)。我遇到了一个使用的代码 <<-
,这在Ruby中意味着什么?代码是
def expectation_message(expectation)
<<-FE
#{expectation.message}
#{expectation.stack}
FE
end
这只是整个代码的一部分。任何帮助,将不胜感激。
有多种方法可以在Ruby中定义多行字符串。这是其中之一。
> name = 'John'
> city = 'Ny'
> multiline_string = <<-EOS
> This is the first line
> My name is #{name}.
> My city is #{city} city.
> EOS
=> "This is the first line\nMy name is John.\nMy city is Ny city.\n"
>
该 EOS
在上面的示例中只是一个约定,您可以使用您喜欢的任何字符串及其不区分大小写。通常是 EOS
手段 End Of String
而且,即使是 -
(破折号)不需要。但是,允许您缩进“此处doc结束”分隔符。请参阅以下示例以了解句子。
2.2.1 :014 > <<EOF
2.2.1 :015"> My first line without dash
2.2.1 :016"> EOF
2.2.1 :017"> EOF
=> "My first line without dash\n EOF\n"
2.2.1 :018 > <<-EOF
2.2.1 :019"> My first line with dash. This even supports spaces before the ending delimiter.
2.2.1 :020"> EOF
=> "My first line with dash. This even supports spaces before the ending delimiter.\n"
2.2.1 :021 >
有关详情,请参阅 https://cbabhusal.wordpress.com/2015/10/06/ruby-multiline-string-definition/
<<FE
(您可以用另一个词替换FE)用于创建多行字符串。 <<-FE
用于在删除结束标记之前创建具有空白的多行字符串。