# A new quotation operator to automatically unindent %q and %Q

**URL:** <https://www.ruby-forum.com/t/a-new-quotation-operator-to-automatically-unindent-q-and-q/151293>\
**Category:** Ruby\
**Created:** [October 19, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/a-new-quotation-operator-to-automatically-unindent-q-and-q/151293 "2008-10-19T23:39:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [October 19, 2008, 11:39pm UTC](https://www.ruby-forum.com/t/a-new-quotation-operator-to-automatically-unindent-q-and-q/151293/1 "2008-10-19T23:39:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [October 20, 2008, 1:40am UTC](https://www.ruby-forum.com/t/a-new-quotation-operator-to-automatically-unindent-q-and-q/151293/2 "2008-10-20T01:40:48Z")

</div>

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:
> 
> 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

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [October 20, 2008, 2:11am UTC](https://www.ruby-forum.com/t/a-new-quotation-operator-to-automatically-unindent-q-and-q/151293/3 "2008-10-20T02:11:13Z")

</div>

On Sun, Oct 19, 2008 at 5:38 PM, Suraj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [October 20, 2008, 4:35am UTC](https://www.ruby-forum.com/t/a-new-quotation-operator-to-automatically-unindent-q-and-q/151293/4 "2008-10-20T04:35:37Z")

</div>

On Oct 19, 5:38 pm, Suraj K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

[RCRchive](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.

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [October 20, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/a-new-quotation-operator-to-automatically-unindent-q-and-q/151293/5 "2008-10-20T14:10:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![snk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@snk](https://www.ruby-forum.com/u/snk)\
**Post date:** [October 21, 2008, 10:28am UTC](https://www.ruby-forum.com/t/a-new-quotation-operator-to-automatically-unindent-q-and-q/151293/6 "2008-10-21T10:28:38Z")

</div>

Suraj K. 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.
