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
[
<<__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”.
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