Marcel Molina Jr.
10/3/2004 6:15:00 AM
On Sun, Oct 03, 2004 at 12:55:51PM +0900, James Edward Gray II wrote:
> Ruby's here-doc syntax baffles me. I don't get the indent the end tag
> feature.
>
> string = <<-END_OF_STRING
> Line one...
> line two...
> line three.
> END_OF_STRING
>
> That leaves all the tabs at the beginning of those lines, which then
> begs the question, why did I bother to indent the end tag?
>
> It would make a lot more sense to me if lines of a here-doc were
> stripped of leading whitespace equal to the indention of the end tag.
> Is there any reason this wasn't done, or isn't practical?
>
> If not, please consider this an official request, by at least one
> coder...
if you want to have your here-doc indented to line up with your code
but don't want the here-doc's content to be indented (not what you are
really asking, i know), there is a pretty ugly way of doing this.
example:
class Blah
def indented_method_def_with_here_doc
string = <<-END_OF_STRING
This text won't have leading white space
even though the here-doc
is indented.
But is the gsubing stuff uglier than breaking
your indentation?
END_OF_STRING
string.gsub(Regexp.new('^' + string[/^\s+/]), '')
end
end
marcel
--
Marcel Molina Jr. <marcel@vernix.org>