Heredocs are a handy way of defining multi-line strings:
instructions = <<-INSTRUCTIONS
Here is a long set of instructions.
You've got to do a lot of things:
1. This thing.
2. That thing.
3. Even more
INSTRUCTIONS
One issue you might encounter is defining a heredoc in some code that is indented:
class Messages
def self.welcome
<<-WELCOME
This is the welcome message.
However, because it's a heredoc, we need to unindent it all the way out :(.
Otherwise Ruby thinks the indentation we include is part of the string.
WELCOME
end
end
How can we have our heredocs and maintain our usual indentation scheme?
strip_heredoc
to the rescue!
class Messages
def self.welcome
<<-WELCOME.strip_heredoc
This is the welcome message.
Since we're using strip_heredoc, we can indent it as usual.
Much nicer, no?
WELCOME
end
end
strip_heredoc
will trim leading whitespace to match the first line, allowing you to keep your code neat and tidy within your method block.