ruby - - 在 ruby​​ Heredoc 中的 << 之后签名

标签 ruby heredoc

在下面的内容中,它们都输出相同的内容。我不喜欢在 << 之后添加 - ,就像 <<-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/

相关文章:

ruby-on-rails - Rails 版本停留在 2.3.5 如何更新到 3.x

ruby-on-rails - Rails 可安装引擎 : how to bundle in each app that uses it?

ruby-on-rails - 如何迭代模型然后在我的 View 中再次迭代?

ruby - 如何在 irb 中抑制 mongod 心跳输出

json - 将 heredoc 声明压缩到 bash 中的一行?

python - zsh:在 zsh 脚本中的heredoc之后转义换行符

ruby - 在指定命名空间类时,我如何 stub 父类?

shell - 是否可以使用 Heredoc 将参数传递给 `runhaskell`?

perl - 从 'use constant' 中的 Perl heredoc 中删除无关空格的 Perl 语法是什么?

perl - 这两个 Perl 片段之间有什么区别?