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