We don’t exactly “need it”, but its nice to have around.
Heredoc has some special properties that can come in handy if you are
using long literal strings in argument lists. For example, you can begin
a Heredoc in an argument list and continue after the list is closed:
foo(‘test’, <<-HERE, ‘test’)
THIS IS A LINE
AND A SECOND
HERE
Taken to the extreme, you can use 3 Heredocs:
foo(<<-FIRST, <<-SECOND, <<-THIRD)
my first text
FIRST
my second text
SECOND
my third text
THIRD
It’s great if you want to copy and paste some text. The text might have
single and double quotes in it, but with a heredoc that doesn’t matter.
Otherwise, you’d have to go through your document, find all the quotes,
and
escape them.
It’s great if you want to copy and paste some text. The text might have
single and double quotes in it, but with a heredoc that doesn’t matter.
Otherwise, you’d have to go through your document, find all the quotes, and
escape them.
Well…
16:11:42 ~$ ruby x
there are “double” and ‘single’
quotes!
And even #{does not} harm.
“\nthere are “double” and ‘single’\nquotes!\nAnd even #{does not}
harm.\n”
16:11:44 ~$ cat -n x
1 str=%q{
2 there are “double” and ‘single’
3 quotes!
4 And even #{does not} harm.
5 }
6 puts str
7 p str
16:11:46 ~$
It’s great if you want to copy and paste some text. The text might have
single and double quotes in it, but with a heredoc that doesn’t matter.
Otherwise, you’d have to go through your document, find all the quotes, and
escape them.
take care of “some text” here. If the text contains your stop word,
you’re gonna have to escape that as well. Or choose a different stop
word then.
On Tue, Jan 31, 2012 at 09:00:06PM +0900, Lucky Nl wrote:
Anyspecific reason to use heredoc concept
I use heredocs in my programs primarily for documentation that is output
in answer to command line options, because:
It looks prettier in the source, and stands out more as separate from
the rest of the code, than other string quoting mechanisms.
It allows me to use both ASCII apostrophes and ASCII double quotes in
these documentation strings without cluttering up the text with escape
character backslashes.
take care of some text here. If the text contains your stop word, youre
gonna have to escape that as well. Or choose a different stop word then.
In practice, I’ve never had that happen, but in any situation where it was
likely, I’d probably have thrown it under END and read it in, or put it
in its own file.
First of all, as this is a literal, this would be a parse error, so it
doesn’t
matter that much. Also, the stop word has to be at the beginning of a
line.
Also, the stop word has to be at the beginning of a line.
<<-HERE
test HERE
HERE
=> “test HERE\n”
Not quite. If you left out the dash, it would have to be at the
beginning. With the dash, however, you can at least indent the
endword. (You can even indent the whole thing, and then trim off the
indentation at the start of each line. See Sven S.'s blog entry
at http://www.bitcetera.com/en/techblog/2009/07/02/heredoc-with-indent-in-ruby/.)
It still has to be the first non-whitespace on the line though.
gonna have to escape that as well. Or choose a different stop word then.
In practice, I’ve never had that happen, but in any situation where it
was
likely, I’d probably have thrown it under END and read it in, or put
it
in its own file.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.