Question

In the followings, both of them outputs the same things. I am not suer the point of having - after <<, like <<-END rather than <

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
Was it helpful?

Solution

The minus sign - after the << characters means you can indent the terminator.

So if the here document starts with @text = <<END, you must terminate with END in the beginning of the line. But with @text = <<-END, you can put space before 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top