Forum: Ruby why we need heredoc

Posted by Lucky Nl (lucky1234)
on 2012-01-31 13:00
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
Posted by Nikolai Weibull (Guest)
on 2012-01-31 13:11
(Received via mailing list)
On Tue, Jan 31, 2012 at 13:00, Lucky Nl <lakshmi27.u@gmail.com> wrote:

> Anyspecific reason to use heredoc concept

puts <<EOA, <<EOB, <<EOC
a
EOA
b
EOB
c
EOC
Posted by Florian Gilcher (skade)
on 2012-01-31 13:25
(Received via mailing list)
On Jan 31, 2012, at 1:00 PM, Lucky Nl wrote:

>    4. Milk.
>    4. 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
Posted by Josh Cheek (josh-cheek)
on 2012-01-31 15:52
(Received via mailing list)
On Tue, Jan 31, 2012 at 6:00 AM, Lucky Nl <lakshmi27.u@gmail.com> wrote:

>    4. Milk.
>    4. 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.
Posted by Robert Klemme (robert_k78)
on 2012-01-31 16:13
(Received via mailing list)
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.

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

:-)

Kind regards

robert
Posted by Adam Prescott (Guest)
on 2012-01-31 17:23
(Received via mailing list)
On Tue, Jan 31, 2012 at 15:12, Robert Klemme 
<shortcutter@googlemail.com>wrote:

>     7  p str
> 16:11:46 ~$


But!

$ ruby -e '%q{{}'
-e:1: unterminated string meets end of file
Posted by "Matthias Wächter" <matthias@waechter.wiz.at> (Guest)
on 2012-01-31 17:40
(Received via mailing list)
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.

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.
Posted by Chad Perrin (Guest)
on 2012-01-31 18:34
(Received via mailing list)
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.
Posted by Josh Cheek (josh-cheek)
on 2012-02-01 01:24
(Received via mailing list)
2012/1/31 Matthias Wchter <matthias@waechter.wiz.at>

> 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.
Posted by Florian Gilcher (skade)
on 2012-02-01 08:32
(Received via mailing list)
On Feb 1, 2012, at 1:22 AM, Josh Cheek 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"
Posted by Dave Aronson (Guest)
on 2012-02-01 16:44
(Received via mailing list)
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.
>
> <<-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 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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.