Forum: Ruby Problem with stripping leading whitespace in here documents

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B351db35040dcc3be001dd51ba798a95?d=identicon&s=25 unknown (Guest)
on 2007-01-25 22:26
(Received via mailing list)
I'm writing a number of test cases.  I would like the input data to
behave like a file, and I would like to include that data within
the test file (in order to reduce the proliferation of files and
eliminate the necessity of tracking an additional, separate file
for each test case).  Similar mechanisms would include BASIC's
DATA statements, or Perl's DATA filehandle.  I don't know of an
analogue in ruby.  Any suggestions?

Instead I am using Bourne shell-type here-documents.  But, I am
encountering a problem.  The Bourne shell supports the concept of
stripping leading white space, and Ruby seems to support the same
notion (from .../doc/ruby18/ruby-man/syntax.html):

    "If the - [is] placed before the delimiter, then all leading
    whitespcae [sic] characters (tabs or spaces) are stripped from
    input lines and the line containing [the] delimiter. This
    allows here-documents within scripts to be indented in a natural
    fashion."

which is the same syntax as the Bourne shell.

But, if I run the following script:

# Start of script:
input1 = <<EOF
abc
def
EOF

input2 = <<-EOF
        ghi
        jkl
EOF

p input1
p input2
# End of script.

I get the following output:

"abc\ndef\n"
"\tghi\n\tjkl\n"

Note that the leading tab has NOT been removed in the here-document
that should be stripped (input2).  (It is a tab in the original file.)

Is this a bug? or am I missing something in the documentation.
I'm using 'ruby 1.8.5 (2006-12-25 patchlevel 12) [i386-freebsd6]'.

(And yes, I know how to strip the leading whitespace with a function
after reading it.  I'm particularly addressing the issue with here
documents.)

Thanks, - dmw
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-25 22:40
(Received via mailing list)
On Jan 25, 12:43 pm, s...@signature.invalid (Douglas Wells) wrote:
> notion (from .../doc/ruby18/ruby-man/syntax.html):
>
>     "If the - [is] placed before the delimiter, then all leading
>     whitespcae [sic] characters (tabs or spaces) are stripped from
>     input lines and the line containing [the] delimiter. This
>     allows here-documents within scripts to be indented in a natural
>     fashion."

Where is this document that you're referring to? I just checked my
(win32 installer) version of Ruby 1.8.5, and I have no document named
'syntax.html' anywhere in the tree of files.
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-25 22:41
(Received via mailing list)
On Jan 25, 12:43 pm, s...@signature.invalid (Douglas Wells) wrote:
>     "If the - [is] placed before the delimiter, then all leading
>     whitespcae [sic] characters (tabs or spaces) are stripped from
>     input lines and the line containing [the] delimiter. This
>     allows here-documents within scripts to be indented in a natural
>     fashion."

Whoa, I've never seen that wording. That is entirely at odds with the
way Ruby behaves; as you note, no whitespace stripping occurs, except
that the '-' character allows the ENDDOC indicator to be indented.

The lack of functionality that happens to be described in the document
that you quote above is what caused me to add this RCR:
http://www.rcrchive.net/rcrs/2
B351db35040dcc3be001dd51ba798a95?d=identicon&s=25 unknown (Guest)
on 2007-01-26 02:21
(Received via mailing list)
In article <1169761184.295076.234450@a75g2000cwd.googlegroups.com>,
"Phrogz" <gavin@refinery.com> writes:
> (win32 installer) version of Ruby 1.8.5, and I have no document named
> 'syntax.html' anywhere in the tree of files.

It was installed on my local system (as part of the ports system
on FreeBSD).  A bit of spelunking shows that it is part of the
'ruby-man' port.  A bit more shows that it came as part of a tarball
named ruby-man-1.4.6.tar.gz, indicating that it might be a bit
long in the tooth.  Checking the page on my systems, I see that
it's fairly old, with the corresponding table of contents being
dated February 1998.

On the other hand, it appears to be the latest "Ruby reference
manual" that I can find.  A Google search for a "ruby reference
manual" leads to
http://www.ruby-doc.org/docs/ruby-doc-bundle/Manua...,
which does contain the quoted text that I posted.

 - dmw
8912aa610b5a39c0749dc1718c797467?d=identicon&s=25 Paul van Delst (Guest)
on 2007-01-26 19:40
(Received via mailing list)
Phrogz wrote:
>
> The lack of functionality that happens to be described in the document
> that you quote above is what caused me to add this RCR:
> http://www.rcrchive.net/rcrs/2

I vote for option 1 of the above proposal, since option 2 would break a
lot of my current
heredoc output.

It would also be nice if one could specify how much of the END token
leading spaces are
removed (or replaced) from each line of the heredoc. Similar to the
strip_output() method
in... ri? (Can't remember exactly)

That is, your

   class Foo
     def bar
       if whee
         print <<+END
         Hello World...
            Are you listening?
         END
       end
     end
   end

produces

Hello World...
    Are you listening?

and then, for example,

   class Foo
     def bar
       if whee
         print <<+(2)END
         Hello World...
            Are you listening?
         END
       end
     end
   end

produces

   Hello World...
      Are you listening?

i.e. the leading spaces are replaced by the number specified after the
+. (I'm lukewarm
about the syntax, but you get the idea)

I realise that would be equivalent to

         print <<+END
           Hello World...
              Are you listening?
         END

but the "<<+(n)TOKEN" functionality would be a really nice feature
because the number of
indent spaces, n, can then be easily passed into methods interpolated in
the heredoc that
produce output you also want to be indented accordingly.  I have that
situation now where
interpolated strings in heredocs are produced in the called method via
another heredoc (I
hope that makes sense.)

cheers,

paulv
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-26 21:30
(Received via mailing list)
On Jan 26, 11:36 am, Paul van Delst <Paul.vanDe...@noaa.gov> wrote:
> I vote for option 1 of the above proposal, since option 2 would break a lot of my 
current
> heredoc output.

[snip additional suggestions and discussion of the RCR]

I'm mighty tempted to ask you for some examples of what your current
Heredoc output looks like and how the proposal would break it.
(However, pretend that I didn't actually just type that.) Instead, I
think that -- to support the RCR process -- I should probably ask you
to create an account at RCRchive.net, subscribe to the mailing list for
that RCR, and post your question again so that I can ask you questions
there.
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-01-26 21:36
(Received via mailing list)
On Jan 25, 5:30 pm, s...@signature.invalid (Douglas Wells) wrote:
> manual" leads 
tohttp://www.ruby-doc.org/docs/ruby-doc-bundle/Manua......,
> which does contain the quoted text that I posted.

Interesting. Definitely, as we've established, not correct information
for the current version of Ruby.

Is anyone around and listening who is old enough to remember 1.4, and
to know if that is how indented heredoc strings worked back then? (I'm
interested because it's very similar to my RCR, and I'd love to know
the reason that the functionality was abandoned, if it was.)
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (Guest)
on 2007-01-27 08:47
(Received via mailing list)
Hi,

At Sat, 27 Jan 2007 05:35:06 +0900,
Phrogz wrote in [ruby-talk:236308]:
> Is anyone around and listening who is old enough to remember 1.4, and
> to know if that is how indented heredoc strings worked back then? (I'm

Nothing has ever changed.  The document is incorrect definitely.
This topic is locked and can not be replied to.