在下面的内容中,它们都输出相同的内容。我不喜欢在 << 之后添加 - ,就像 <<-END 而不是 <
class Poem
def initialize
@text = <<END
"Faith" is a fine invention
When Gentlemen can see
But Microscopes are prudent
In an Emergency.
(Emily Dickinson 1830-1886)
END
end
def recite
puts @text
end
end
poem = Poem.new
poem.recite
class Poem1
def initialize
@text = <<-END
"Faith" is a fine invention
When Gentlemen can see
But Microscopes are prudent
In an Emergency.
(Emily Dickinson 1830-1886)
END
end
def recite
puts @text
end
end
poem1 = Poem1.new
poem1.recite
最佳答案
减号-
<<
之后字符意味着您可以缩进终止符。
如果这里的文档以 @text = <<END
开头,您必须以 END
终止在该行的开头。但随着@text = <<-END
,可以在 END
前面加空格.
@text = <<-END
"Faith" is a fine invention
When Gentlemen can see
But Microscopes are prudent
In an Emergency.
(Emily Dickinson 1830-1886)
END
#^^^ Here white space
关于ruby - - 在 ruby Heredoc 中的 << 之后签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19994898/