Problem with stripping leading whitespace in here documents

D

Douglas Wells

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
 
P

Phrogz

"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
 
P

Phrogz

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

Douglas Wells

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.

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/Manual/man-1.4/syntax.html#here_doc,
which does contain the quoted text that I posted.

- dmw
 
P

Paul van Delst

Phrogz said:
"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

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
 
P

Phrogz

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

Phrogz

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 tohttp://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/syntax.ht...,
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.)
 
N

Nobuyoshi Nakada

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top