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 Jan 31, 2012, at 1:00 PM, Lucky Nl wrote:
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.
"
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 6:00 AM, Lucky Nl <lakshmi27.u@gmail.com> wrote:
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.
"
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 Tue, Jan 31, 2012 at 09:00:06PM +0900, Lucky Nl wrote:
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 ~$
Kind regards
robert
···
On Tue, Jan 31, 2012 at 3:50 PM, Josh Cheek <josh.cheek@gmail.com> 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 31.01.2012 15:50, Josh Cheek 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.
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.
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.
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.
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.
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.
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 Schwyn'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
···
On Wed, Feb 1, 2012 at 02:31, Florian Gilcher <flo@andersground.net> wrote:
Also, the stop word has to be at the beginning of a line.