Why we need heredoc

In ruby we have the concpet “heredoc” to handle multiple line strings

We can handle that with doublequotes
for example
x = "Grocery list

1. Salad mix.
2. Strawberries.
3. Cereal.
4. Milk.
   "

puts x

OUTPUT:
Grocery list

1. Salad mix.
2. Strawberries.
3. Cereal.
4. Milk.

Anyspecific reason to use heredoc concept

On Tue, Jan 31, 2012 at 13:00, Lucky Nl [email protected] wrote:

Anyspecific reason to use heredoc concept

puts <<EOA, <<EOB, <<EOC
a
EOA
b
EOB
c
EOC

On Jan 31, 2012, at 1:00 PM, Lucky Nl wrote:

  1. Milk.
  2. Milk.

Anyspecific reason to use heredoc concept

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

Regards,
Florian

On Tue, Jan 31, 2012 at 6:00 AM, Lucky Nl [email protected] wrote:

  1. Milk.
  2. Milk.

Anyspecific reason to use heredoc concept


Posted via http://www.ruby-forum.com/.

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.

On Tue, Jan 31, 2012 at 3:50 PM, Josh C. [email protected]
wrote:

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 ~$

:slight_smile:

Kind regards

robert

On Tue, Jan 31, 2012 at 15:12, Robert K.
[email protected]wrote:

7  p str

16:11:46 ~$

But!

$ ruby -e ‘%q{{}’
-e:1: unterminated string meets end of file

On 31.01.2012 15:50, Josh C. wrote:

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:

  1. It looks prettier in the source, and stands out more as separate from
    the rest of the code, than other string quoting mechanisms.

  2. 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.

On Feb 1, 2012, at 1:22 AM, Josh C. wrote:

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.

<<-HERE
test HERE
HERE
=> “test HERE\n”

On Wed, Feb 1, 2012 at 02:31, Florian G. [email protected]
wrote:

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.

-Dave

2012/1/31 Matthias W. [email protected]

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.