Forum: Ruby a new quotation operator to automatically unindent %q and %Q

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.
Bc368ef524130e8d0deb386de961e24a?d=identicon&s=25 Suraj Kurapati (snk)
on 2008-10-19 23:39
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?

Thanks for your consideration.
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2008-10-20 01:40
(Received via mailing list)
On Sun, 19 Oct 2008 16:38:05 -0500, Suraj Kurapati 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:
>
>   amount_to_unindent = input_string[/\A(?:\r?\n)+([ \t]+)(?=\S)/, 1]
>   input_string.gsub! /^#{amount_to_unindent}/, ''
>
> What do you think?
>
> Thanks for your consideration.

Use the facets gem's String#margin instead?

--Ken
D337e2cf7c6d03caac6988d83e0e7f66?d=identicon&s=25 Michael Guterl (mguterl)
on 2008-10-20 02:11
(Received via mailing list)
On Sun, Oct 19, 2008 at 5:38 PM, Suraj Kurapati <snk@gna.org> 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:
>
>  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.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Thomas Sawyer (7rans)
on 2008-10-20 04:35
(Received via mailing list)
On Oct 19, 5:38 pm, Suraj Kurapati <s...@gna.org> 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:
>
>   amount_to_unindent = input_string[/\A(?:\r?\n)+([ \t]+)(?=\S)/, 1]
>   input_string.gsub! /^#{amount_to_unindent}/, ''
>
> What do you think?

Hi--

This is not an uncommon request. Consider:

  http://rcrchive.net/rcrs/2

Long long ago I proposed %l and %L:

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

T.
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2008-10-20 14:10
(Received via mailing list)
On Sun, 19 Oct 2008 21:33:56 -0500, Trans wrote:

>>         %q{
>>           Unless we manually remove it,
>> I would like to propose a new set of string quotation operators %s and
>
>            |
> But matz does not seem to see merit in any of this. I'm not sure why.
>
> T.

Probably because it can easily be done in a library. See the facets
gem's
String#margin
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-10-21 10:28
Suraj Kurapati wrote:
> What do you think?

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 topic is locked and can not be replied to.