Michael Guterl
10/20/2008 12:10:00 AM
On Sun, Oct 19, 2008 at 5:38 PM, Suraj Kurapati <snk@gna.org> wrote:
> Hello everyone,
>
> 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:
>
> amount_to_unindent = input_string[/\A(?:\r?\n)+([ \t]+)(?=\S)/, 1]
> input_string.gsub! /^#{amount_to_unindent}/, ''
>
> What do you think?
>
%s is already in use for symbols.