Replacing '

Hi

Is there a nicer way than "\\\\'" for replacing ' with \' ?

"ab'cd".gsub(/'/,'\\'+"'") does interpret \' ...

thanks
Opti

Do you consider this nicer?

"ab'cd".gsub(/'/) { "\\'” }

Hope this helps,

Mike

···

On Mar 12, 2017, at 10:09 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi

Is there a nicer way than "\\\\'" for replacing ' with \' ?

"ab'cd".gsub(/'/,'\\'+"'") does interpret \' ...

thanks
Opti

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Hi

Why is there no difference between '\\' and "\\" ?
(Thought " ..." would need doubled \\ ???

thanks
Opti

Hi Mike!
Yes, that would be nicer, but it also doesn't work (as the + veriant) - try yourself
So pls a working solution ;))

Opti

···

On 2017-03-12 18:16, Mike Stok wrote:

On Mar 12, 2017, at 10:09 AM, Die Optimisten >> <inform@die-optimisten.net <mailto:inform@die-optimisten.net>> wrote:

Hi

Is there a nicer way than "\\\\'" for replacing ' with \' ?

"ab'cd".gsub(/'/,'\\'+"'") does interpret \' ...

thanks
Opti

Do you consider this nicer?

"ab'cd".gsub(/'/) { "\\'” }

Hope this helps,

Mike

The \ is used to introduce special sequences in both single and double quoted strings. In single quoted strings it’s used to escape an embedded single quote, \', and to escape itself \\.

Hope this helps,

Mike

···

On Mar 12, 2017, at 10:16 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi

Why is there no difference between '\\' and "\\" ?
(Thought " ..." would need doubled \\ ???

thanks
Opti

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

I believe my solution does work, there’s a reason I used the block:

$ pry
[1] pry(main)> "ab'cd".gsub(/'/) { "\\'" }
=> "ab\\'cd"
[2] pry(main)> "ab'cd".gsub(/'/, "\\'")
=> "abcdcd"

Mike

···

On Mar 12, 2017, at 2:00 PM, Die Optimisten <inform@die-optimisten.net> wrote:

On 2017-03-12 18:16, Mike Stok wrote:

On Mar 12, 2017, at 10:09 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi

Is there a nicer way than "\\\\'" for replacing ' with \' ?

"ab'cd".gsub(/'/,'\\'+"'") does interpret \' ...

thanks
Opti

Do you consider this nicer?

"ab'cd".gsub(/'/) { "\\'” }

Hope this helps,

Mike

Hi Mike!
Yes, that would be nicer, but it also doesn't work (as the + veriant) - try yourself
So pls a working solution ;))

Opti

--

Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.

Hello!
ups - I was manacled? hypnotized by / cognitive bound to the very specific \\' - behavior
and didn't realize the block - sorry!
Thanks for eval, for now I'll do it that way. - Yes it can be dangerous...

Opti

···

On 2017-03-12 19:12, Mike Stok wrote:

On Mar 12, 2017, at 2:00 PM, Die Optimisten <inform@die-optimisten.net> wrote:

On 2017-03-12 18:16, Mike Stok wrote:

On Mar 12, 2017, at 10:09 AM, Die Optimisten <inform@die-optimisten.net> wrote:

Hi

Is there a nicer way than "\\\\'" for replacing ' with \' ?

"ab'cd".gsub(/'/,'\\'+"'") does interpret \' ...

thanks
Opti

I think this is not the explanation that OP is looking for. The real
reason is that for convenience Matz had decided that some sequences do
work without a second slash, e.g.

irb(main):006:0> '\1'
=> "\\1"
irb(main):007:0> '\\1'
=> "\\1"

This is convenient when using String#gsub for example

irb(main):008:0> "foobar".gsub(/.(o+)./, '<\1>')
=> "<oo>ar"
irb(main):009:0> "foobar".gsub(/.(o+)./, '<\\1>')
=> "<oo>ar"

I found it confusing initially and prefer to use \\1 instead of \1
because that way it is more consistent and easier to read. The
sequence \1 looks like \n which, in a doubly quoted string, is
actually a newline.

Kind regards

robert

···

On Sun, Mar 12, 2017 at 6:20 PM, Mike Stok <mike@stok.ca> wrote:

On Mar 12, 2017, at 10:16 AM, Die Optimisten <inform@die-optimisten.net>

Why is there no difference between '\\' and "\\" ?
(Thought " ..." would need doubled \\ ???

The \ is used to introduce special sequences in both single and double
quoted strings. In single quoted strings it’s used to escape an embedded
single quote, \', and to escape itself \\.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

I recommend against using the block form. It incurs unnecessary
overhead and is not needed for this particular use case since the
replacement does not need to be calculated based on the individual
match. If you understand that there are really two levels of escaping
going on (String literals and gsub) the solution is clear immediately:
you need four backslashes to get a literal backslash in the
replacement. Add one backslash for escaping the ' in a single quoted
string and alas:

irb(main):001:0> s="a'b"
=> "a'b"
irb(main):002:0> s.gsub /'/, '\\\\\''
=> "a\\'b"
irb(main):003:0> puts s.gsub /'/, '\\\\\''
a\'b
=> nil

Btw. this has been discussed _numerous_ times on this very list.

Kind regards

robert

···

On Sun, Mar 12, 2017 at 9:16 PM, Die Optimisten <inform@die-optimisten.net> wrote:

On 2017-03-12 19:12, Mike Stok wrote:

On Mar 12, 2017, at 2:00 PM, Die Optimisten <inform@die-optimisten.net> >>> wrote:

On 2017-03-12 18:16, Mike Stok wrote:

On Mar 12, 2017, at 10:09 AM, Die Optimisten >>>>> <inform@die-optimisten.net> wrote:

Hi

Is there a nicer way than "\\\\'" for replacing ' with \' ?

"ab'cd".gsub(/'/,'\\'+"'") does interpret \' ...

Hello!
ups - I was manacled? hypnotized by / cognitive bound to the very specific
\\' - behavior
and didn't realize the block - sorry!
Thanks for eval, for now I'll do it that way. - Yes it can be dangerous...

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Hi
Thanks for that info - yes, it's confusing for me - less possibilties would be easier to understand (and remember!)
It'll take some more effort to reach the next step...
Is there a (complete) doku with all string expansion details (explained at one place) ?
$1 vs \1, \xyz (hex, oct, special chars and escaping and '...' vs "..." (and perhaps something more I don't know of)
DidnÄt find any website with detailed, complete infos.

thank you
Opti

···

On 2017-03-13 09:02, Robert Klemme wrote:

I think this is not the explanation that OP is looking for. The real
reason is that for convenience Matz had decided that some sequences do
work without a second slash, e.g.

irb(main):006:0> '\1'
=> "\\1"
irb(main):007:0> '\\1'
=> "\\1"

This is convenient when using String#gsub for example

irb(main):008:0> "foobar".gsub(/.(o+)./, '<\1>')
=> "<oo>ar"
irb(main):009:0> "foobar".gsub(/.(o+)./, '<\\1>')
=> "<oo>ar"

I found it confusing initially and prefer to use \\1 instead of \1
because that way it is more consistent and easier to read. The
sequence \1 looks like \n which, in a doubly quoted string, is
actually a newline.

Kind regards

robert

You could try Programming Ruby: The Pragmatic Programmer's Guide - maybe it is
in there. Even though it is quite dated I don't think there has been a
change in this area.

Kind regards

robert

···

On Tue, Mar 14, 2017 at 2:59 PM, Die Optimisten <inform@die-optimisten.net> wrote:

Is there a (complete) doku with all string expansion details (explained at
one place) ?
$1 vs \1, \xyz (hex, oct, special chars and escaping and '...' vs "..."
(and perhaps something more I don't know of)
DidnÄt find any website with detailed, complete infos.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/