[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

weird backsplash behaviour inside single quotes

Michal Suchanek

7/25/2008 1:35:00 PM

Hello

I observe that backslashed characters normally produce backaslash plus
the character inside single quotes.


The understandable exception are single quotes. However, in Ruby
double backslash also produces a single backslash.

irb(main):008:0> '\a'
=> "\\a"
irb(main):009:0> '\\a'
=> "\\a"
irb(main):010:0> '\\\a'
=> "\\\\a"
irb(main):011:0> '\\\\a'
=> "\\\\a"
irb(main):012:0>


I think this is needlessly confusing. Usually single quotes are as
literal as possible.

Thanks

Michal

12 Answers

Jano Svitok

7/25/2008 2:15:00 PM

0

On Fri, Jul 25, 2008 at 15:34, Michal Suchanek <hramrach@centrum.cz> wrote:
> Hello
>
> I observe that backslashed characters normally produce backaslash plus
> the character inside single quotes.
>
>
> The understandable exception are single quotes. However, in Ruby
> double backslash also produces a single backslash.
>
> irb(main):008:0> '\a'
> => "\\a"
> irb(main):009:0> '\\a'
> => "\\a"
> irb(main):010:0> '\\\a'
> => "\\\\a"
> irb(main):011:0> '\\\\a'
> => "\\\\a"
> irb(main):012:0>
>
>
> I think this is needlessly confusing. Usually single quotes are as
> literal as possible.

\ is escaped because it is used to escape single quotes.

Todd Benson

7/25/2008 2:28:00 PM

0

On Fri, Jul 25, 2008 at 8:34 AM, Michal Suchanek <hramrach@centrum.cz> wrote:
> Hello
>
> I observe that backslashed characters normally produce backaslash plus
> the character inside single quotes.
>
>
> The understandable exception are single quotes. However, in Ruby
> double backslash also produces a single backslash.
>
> irb(main):008:0> '\a'
> => "\\a"
> irb(main):009:0> '\\a'
> => "\\a"
> irb(main):010:0> '\\\a'
> => "\\\\a"
> irb(main):011:0> '\\\\a'
> => "\\\\a"
> irb(main):012:0>
>
>
> I think this is needlessly confusing. Usually single quotes are as
> literal as possible.

The backslash is a tool to escape the single quote inside a quote,
therefore it must escape itself as well.

Todd

Todd Benson

7/25/2008 2:33:00 PM

0

On Fri, Jul 25, 2008 at 9:33 AM, Todd Benson <caduceass@gmail.com> wrote:
> The backslash is a tool to escape the single quote inside a quote,
> therefore it must escape itself as well.

I should be more clear. If you have a symbol that performs a function
withing a domain (the quote) and you want to have the symbol not
perform that function, but just be part of the domain, you have to use
that symbol (escape) on itself.

whew,
Todd

Michal Suchanek

7/25/2008 3:09:00 PM

0

On 25/07/2008, Todd Benson <caduceass@gmail.com> wrote:
> On Fri, Jul 25, 2008 at 9:33 AM, Todd Benson <caduceass@gmail.com> wrote:
> > The backslash is a tool to escape the single quote inside a quote,
> > therefore it must escape itself as well.
>
>
> I should be more clear. If you have a symbol that performs a function
> withing a domain (the quote) and you want to have the symbol not
> perform that function, but just be part of the domain, you have to use
> that symbol (escape) on itself.
>
well, there's no function to perform, there's no quote. And if we do
not distinguish between situation when there is a quote or not then
backslashing any other letter should also produce the letter. But then
we will lose the reason for which single quotes were introduced in the
first place: writing less backslashes.

Still this does not work well even now. There are less backslashes
needed to write \\something\something inside single quotes but the
added characters aren't balanced: something\something can be written
as is but for the double backslash a quadruple backslash is required.

That's a confusing inconsistency in my view: if I write 'a\b' and it
results in the exact value that is inside the quotes it is unexpected
and inconsistent that I need '\\\\a\b' to write string with three
backslashes. Perhaps the only solution would be to forbid quoting
inside single quotes completely. Admittedly if only a backslash
followed by a single quote was special writing strings with a trailing
backslash would not be possible with single quotes.

Thanks

Michal

Pit Capitain

7/25/2008 3:26:00 PM

0

2008/7/25 Michal Suchanek <hramrach@centrum.cz>:
> .... But then
> we will lose the reason for which single quotes were introduced in the
> first place: writing less backslashes.

Michal, where have you got this from? I always thought the reason for
single quoted strings was that there's no string interpolation
happening for them.

Regards,
Pit

Sebastian Hungerecker

7/25/2008 4:53:00 PM

0

Pit Capitain wrote:
> writing less backslashes.
>
> Michal, where have you got this from? I always thought the reason for
> single quoted strings was that there's no string interpolation
> happening for them.

Yes, but you can get that with double quotes too:
"\#{this string is not interpolated}"
So what's the upside of '\#{this string is not interpolated}'? Right. No
backslash. I think, that's what Michal meant.

--
NP: Explosions in the Sky - Day 7
Jabber: sepp2k@jabber.org
ICQ: 205544826

Sebastian Hungerecker

7/25/2008 4:59:00 PM

0

Sebastian Hungerecker wrote:
> So what's the upside of '\#{this string is not interpolated}'? Right. No
> backslash.

Ok, I guess that point would have come across better had there actually not
been a backslash in there...

--
NP: Eisregen - Eispalast
Jabber: sepp2k@jabber.org
ICQ: 205544826

Todd Benson

7/25/2008 8:17:00 PM

0

On Fri, Jul 25, 2008 at 10:09 AM, Michal Suchanek <hramrach@centrum.cz> wrote:
> On 25/07/2008, Todd Benson <caduceass@gmail.com> wrote:
>> On Fri, Jul 25, 2008 at 9:33 AM, Todd Benson <caduceass@gmail.com> wrote:
>> > The backslash is a tool to escape the single quote inside a quote,
>> > therefore it must escape itself as well.
>>
>>
>> I should be more clear. If you have a symbol that performs a function
>> withing a domain (the quote) and you want to have the symbol not
>> perform that function, but just be part of the domain, you have to use
>> that symbol (escape) on itself.
>>
> well, there's no function to perform, there's no quote. And if we do
> not distinguish between situation when there is a quote or not then
> backslashing any other letter should also produce the letter. But then
> we will lose the reason for which single quotes were introduced in the
> first place: writing less backslashes.

Simple. What if you're backslash comes at the end of a single-quoted string?

Todd

Todd Benson

7/25/2008 9:22:00 PM

0

On Fri, Jul 25, 2008 at 3:22 PM, Todd Benson <caduceass@gmail.com> wrote:
> Simple. What if you're backslash comes at the end of a single-quoted string?

Beating a dead horse examples...

s = 'I'm not here'; puts s #doesn't work, obviously
# get out of this in irb with a single '
s = 'I\'m not here'; puts s #works
s = 'I\'m not here \'; puts s #doesn't work, obviously
# get out of this in irb with a single '
s = 'I\'m not here \\'; puts s #works

There are a bunch of other examples I probably could come up with.

It would be inconvenient, I think, to make everything inside what you
_assume_ are the two outermost single quotes to be "literal". I
imagine it would make execution slower, as well.

Todd

Pit Capitain

7/27/2008 7:44:00 AM

0

2008/7/25 Sebastian Hungerecker <sepp2k@googlemail.com>:
> Yes, but you can get that with double quotes too:
> "\#{this string is not interpolated}"
> So what's the upside of '\#{this string is not interpolated}'? Right. No
> backslash. I think, that's what Michal meant.

Sebastian, if you have a dial with a range from 1 to 9 and you see
someone turning it from 1 to 6, you can't say to him "you have to turn
the dial farther up to 9 because this has been your goal".

I still think that the reason for having single quoted strings is to
have them doing less of things like string interpolation than double
quoted strings. Yes, you can do the same with double quoted strings by
adding more backslashes. But for me, minimizing the number of
backslashes (turning the dial to 9) has never been the goal of single
quoted strings. As Michal has shown, then they should behave
differently than they do now.

This is why I have been asking him why he thought that the goal was to
minimize the number of backslashes.

Regards,
Pit