weird backsplash behaviour inside single quotes

M

Michal Suchanek

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
 
J

Jano Svitok

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

Todd Benson

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
 
T

Todd Benson

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
 
M

Michal Suchanek

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
 
P

Pit Capitain

2008/7/25 Michal Suchanek said:
.... 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
 
S

Sebastian Hungerecker

Pit said:
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.
 
S

Sebastian Hungerecker

Sebastian said:
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...
 
T

Todd Benson

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
 
T

Todd Benson

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
 
P

Pit Capitain

2008/7/25 Sebastian Hungerecker said:
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
 
M

Michal Suchanek

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.

Because I do not see other goal the single quoted strings might
possibly have. If we did not want to avoid backslashes we could use
double quoted strings all the time.

The problem with the current state is that the single quoted strings
are not a good tool. There is no simple rule of thumb for using them.
You have to repeat all the thought that went into designing them
before you can understand how they work. And you are likely to find
out only when they do not work the way you wanted.

In contrast, double quoted strings have some special characters which
most people use so they are aware of them, and if you do not want them
to be special you escape them with a backslash. It's clear, works the
same all the time, just way too many backslashes in some cases.

Perhaps this would be for the ruby wishlist thread: better
single-quoted strings ...

Thanks

Michal
 
T

Todd Benson

Because I do not see other goal the single quoted strings might
possibly have. If we did not want to avoid backslashes we could use
double quoted strings all the time.

Convenience.

Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top