Many times I have faced the situation of embedding multi-line strings in
my programs while wanting to preserve the indentation of the surrounding
code:
module Foo
class Bar
def to_s
%q{
This block of text will,
unfortunately,
contain the indentation of
the
surrounding
code!
Unless we manually remove it,
as shown by the gsub() below:
}.gsub(/^ /, '')
end
end
end
The same problem occurs for “here documents” as well. We are forced to
manually remove the indentation.
I would like to propose a new set of string quotation operators %s and
%S which behave just like %q and %Q respectively, except that they are
automatically unindented by the Ruby interpreter using the first line of
non-whitespace text:
On Sun, 19 Oct 2008 16:38:05 -0500, Suraj K. wrote:
This block of text will,
as shown by the gsub() below:
%S which behave just like %q and %Q respectively, except that they are
automatically unindented by the Ruby interpreter using the first line of
non-whitespace text:
This block of text will,
as shown by the gsub() below:
%S which behave just like %q and %Q respectively, except that they are
automatically unindented by the Ruby interpreter using the first line of
non-whitespace text:
This block of text will,
as shown by the gsub() below:
%S which behave just like %q and %Q respectively, except that they are
automatically unindented by the Ruby interpreter using the first line of
non-whitespace text:
%l{
|This block of text will,
|
| unfortunately,
|
|contain the indentation of
|
| the
| surrounding
| code!
|
|Unless we manually remove it,
|as shown by the gsub() below:
}
But matz does not seem to see merit in any of this. I’m not sure why.
Just an observation: it will require all the quoted lines to use tabs
and spaces for indenting in an exactly consistent way, otherwise it
probably won’t work.
Using an explicit marker like | is probably better for that reason.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.