2

在下文中,它们都输出相同的东西。我不是在 << 之后有 - 的意义,比如 <<-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
4

1 回答 1

4

-字符后的减号<<表示您可以缩进终止符。

因此,如果此处的文档以 开头@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
于 2013-11-15T06:35:51.383 回答