Weird backsplash behaviour inside single quotes

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

On Fri, Jul 25, 2008 at 15:34, Michal S. [email protected]
wrote:

=> “\a”
literal as possible.
\ is escaped because it is used to escape single quotes.

On Fri, Jul 25, 2008 at 9:33 AM, Todd B. [email protected]
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

On 25/07/2008, Todd B. [email protected] wrote:

On Fri, Jul 25, 2008 at 9:33 AM, Todd B. [email protected] 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

On Fri, Jul 25, 2008 at 8:34 AM, Michal S. [email protected]
wrote:

=> “\a”
literal as possible.
The backslash is a tool to escape the single quote inside a quote,
therefore it must escape itself as well.

Todd

Pit C. 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.

2008/7/25 Michal S. [email protected]:

… 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

On Fri, Jul 25, 2008 at 10:09 AM, Michal S. [email protected]
wrote:

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

Sebastian H. 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…

On Fri, Jul 25, 2008 at 3:22 PM, Todd B. [email protected]
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

2008/7/25 Sebastian H. [email protected]:

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

On 27/07/2008, Pit C. [email protected] wrote:

the dial farther up to 9 because this has been your goal".
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

On Sun, Jul 27, 2008 at 6:45 AM, Michal S. [email protected]
wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs