[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

a new quotation operator to automatically unindent %q and %Q

Suraj Kurapati

10/19/2008 9:38:00 PM

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.
--
Posted via http://www.ruby-....

5 Answers

Ken Bloom

10/19/2008 11:12:00 PM

0

On Sun, 19 Oct 2008 16:38:05 -0500, Suraj Kurapati 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?
>
> Thanks for your consideration.

Use the facets gem's String#margin instead?

--Ken

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Michael Guterl

10/20/2008 12:10:00 AM

0

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.

Trans

10/20/2008 2:34:00 AM

0



On Oct 19, 5:38=A0pm, Suraj Kurapati <s...@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:
>
> =A0 module Foo
> =A0 =A0 class Bar
> =A0 =A0 =A0 def to_s
> =A0 =A0 =A0 =A0 %q{
> =A0 =A0 =A0 =A0 =A0 This block of text will,
>
> =A0 =A0 =A0 =A0 =A0 =A0 unfortunately,
>
> =A0 =A0 =A0 =A0 =A0 contain the indentation of
>
> =A0 =A0 =A0 =A0 =A0 =A0 the
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 surrounding
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 code!
>
> =A0 =A0 =A0 =A0 =A0 Unless we manually remove it,
> =A0 =A0 =A0 =A0 =A0 as shown by the gsub() below:
>
> =A0 =A0 =A0 =A0 }.gsub(/^ =A0 =A0 =A0 =A0 =A0/, '')
> =A0 =A0 =A0 end
> =A0 =A0 end
> =A0 end
>
> The same problem occurs for "here documents" as well. =A0We 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:
>
> =A0 amount_to_unindent =3D input_string[/\A(?:\r?\n)+([ \t]+)(?=3D\S)/, 1=
]
> =A0 input_string.gsub! /^#{amount_to_unindent}/, ''
>
> What do you think?

Hi--

This is not an uncommon request. Consider:

http://rcrchive....

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.

Ken Bloom

10/20/2008 11:40:00 AM

0

On Sun, 19 Oct 2008 21:33:56 -0500, Trans wrote:

> On Oct 19, 5:38 pm, Suraj Kurapati <s...@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?
>
> Hi--
>
> This is not an uncommon request. Consider:
>
> http://rcrchive....
>
> 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.

Probably because it can easily be done in a library. See the facets gem's
String#margin

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Brian Candler

10/21/2008 8:27:00 AM

0

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.
--
Posted via http://www.ruby-....