How To Separate 'Here Documents'

Hello,

I love ‘here documents’, I use them everywhere I can. But when I try to
store them in an array, I don’t know what separator to use. Ruby does
not accept the comma.

[
<<__HERE_DOCUMENT_1
As I told you, a Hamster
__HERE_DOCUMENT_1

what separator keeps here documents apart?

<<__HERE_DOCUMENT_2
We all know that a Zebra
__HERE_DOCUMENT_2

what separator keeps here documents apart?

<<__HERE_DOCUMENT_3
They look cute, but a Giraffe
__HERE_DOCUMENT_3
].each do|a|
puts “#{a} is a dangerous animal!”
end

Well, do you want this?

[
<<__HERE_DOCUMENT_1,
As I told you, a Hamster
__HERE_DOCUMENT_1

<<__HERE_DOCUMENT_2,
We all know that a Zebra
__HERE_DOCUMENT_2

<<__HERE_DOCUMENT_3,
They look cute, but a Giraffe
__HERE_DOCUMENT_3
]
.each {|a| p “#{a} is a dangerous animal!” }

#------------------------------------
“As I told you, a Hamster\n is a dangerous animal!”
“We all know that a Zebra\n is a dangerous animal!”
“They look cute, but a Giraffe\n is a dangerous animal!”
#------------------------------------

the comma should be after the “<<__HERE_DOCUMENT_n”.

Agent M. wrote in post #1006097:

Hello,

I love ‘here documents’, I use them everywhere I can. But when I try to
store them in an array, I don’t know what separator to use. Ruby does
not accept the comma.

Remember, your goal is to write code that other people can read, and
code that you yourself will be able to read 6 months later–not exploit
tricky aspects of a programming language. Other people may not be as
familiar with the intricacies of heredocs as you are. In my opinion,
the only time you should use a heredoc is like this:

str =<<END_OF_STRING
line 1
line 2
line 3
END_OF_STRING

do_stuff(my_str)

That sounds like a good advice. Here-documents can be really nice if you
generate code, for example:

def numbered_actions

def numbered_actions
0.upto(@count).collect do|a|
<<HERE_DOCUMENT

/*
Java comments and code formatted the easy way
Action_#{a} new and adventurous
*/

//Action_#{a}
public static final javax.swing.AbstractAction Action_#{a}=new
javax.swing.AbstractAction()
{{
putValue(javax.swing.Action.ACTION_COMMAND_KEY,“Action_#{a}”);
putValue(javax.swing.Action.LONG_DESCRIPTION, “LONG DESCRIPTION:
Action_#{a}”);
putValue(javax.swing.Action.MNEMONIC_KEY,
java.awt.event.KeyEvent.VK_0);
putValue(javax.swing.Action.NAME, “NAME Action_#{a}”);
putValue(javax.swing.Action.SHORT_DESCRIPTION, “Tooltip
Action_#{a}”);
putValue(javax.swing.Action.SMALL_ICON,
ImageIcon.MIRROR_HORIZONTAL);
}
public void actionPerformed(java.awt.event.ActionEvent a)
{
System.out.println(“Action.Action_#{a}.actionPerformed()”);
javax.swing.JOptionPane.showMessageDialog(null,“Action_#{a}”,“MyApp”,javax.swing.JOptionPane.INFORMATION_MESSAGE);
}};

HERE_DOCUMENT
end
end

def numbered_actions

A ‘here document’ is useless when the granularity is too small.