"daz" <dooby / d10.karoo.co.uk> schrieb im Newsbeitrag
news:02GdnT7dtOarqXPdSa8jmA / karoo.co.uk...
>
> Richard Lionheart wrote:
> > Hi All,
> >
> > The following Ruby script displays use of a pattern to remove a
superfluous
> > backslash in a string if present.  It works as intended with two "toy"
> > situations,  but fails when applied to an environment string running
under
> > Windows.
> >
> > Can anyone tell me where I erred?
> >
> >
> > s2.chop! if s2.reverse =~ /^\\\\/
> >
> > sCookiesPath.chop! if sCookiesPath.reverse !~ /^\\\\/
>
> You changed =~ to !=
> The reversed path doesn't begin with double slash,
> so you chop! the single slash.
>
>
> ["xyz\\", "xyz\\\\"].each do |s|
>   puts '----------', s
>   s.chop! if s.reverse =~ /^\\\\/
>   puts s
> end
>
>
> # Also:
> #   s.sub!(/\\\\\Z/, '\\')
> # works for:
> #   s.chop! if s.reverse =~ /^\\\\/

It seems to me quite inefficient to reverse a string only to match it with
"^".

s.chop! if /\\$/ =~ s

I guess another efficient method is

s.slice!(-1,1) if s[-1] == ?\\

Regards

    robert