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?
TIA,
Richard
============ Ruby pgm ===========
# Executed under Windows 2000
s1 = "ab\\\\"
puts "s1 before: \"#{s1}\" (two backslashes)"
s1.chop! if s1.reverse =~ /^\\\\/
puts "s1 after: \"#{s1}\" (redced to a single backslash)"
"Richard Lionheart" <NoOne@Nowhere.com> schrieb im Newsbeitrag
news:ycmdnde8YJUTv3Pd4p2dnA@comcast.com...
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?
You rather want str.gsub!(/\\$/, '') than str.chop! if you want to remove
trailing backslashes.
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 =~ /^\\\\/
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?
TIA,
Richard
============ Ruby pgm ===========
# Executed under Windows 2000
s1 = "ab\\\\"
puts "s1 before: \"#{s1}\" (two backslashes)"
s1.chop! if s1.reverse =~ /^\\\\/
puts "s1 after: \"#{s1}\" (redced to a single backslash)"
Like my reply to Robert, I originally sent this reply directly to you rather
than the group. It's repeated here in case the first one got lost in your
"bit bucket." and to
mention an adaptation for the problem at hand
Was that a trick question ?
No, it was my stupidity late at night. But it elicited a fast and succinct
correction from Daz and a more efficient (hence more correct, IMHO) approach
from Robert.
So aside from having my problem solved, I got a little more Ruby tutoring.
Not bad for a simple NG post, eh?
Best wishes,
Richard
···
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.713 / Virus Database: 469 - Release Date: 7/7/2004
I originally sent this reply directly to you rather than the group. It's
repeated here in case the first one got lost in your "bit bucket" and to
mention an adaptation for the problem at hand
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] == ?\\
Excellent guidance!! In this case, I'll employ the first since it involves
fewer keystrokes, though I expect the second might be a microsecond faster.
My goal was to chop a final backslash it was immediately preceded by another
backslash. So I'll use:
s.chop! if /\\\\$/ =~ s
Again, thanks for your insight!
Regards,
Richard
···
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.713 / Virus Database: 469 - Release Date: 7/7/2004
"Richard Lionheart" <NoOne@Nowhere.com> schrieb im Newsbeitrag
news:E5CdncY10a8NN3PdRVn-hA@comcast.com...
Hi Robert,
I originally sent this reply directly to you rather than the group.
It's
repeated here in case the first one got lost in your "bit bucket" and to
mention an adaptation for the problem at hand
> 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] == ?\\
Excellent guidance!! In this case, I'll employ the first since it
involves
fewer keystrokes, though I expect the second might be a microsecond
faster.
My goal was to chop a final backslash it was immediately preceded by
another
backslash.
Ok, then my suggestion was not a solution to your problem.
> My goal was to chop a final backslash it was immediately preceded
> by another backslash.
Ok, then my suggestion was not a solution to your problem.
Au contraire ... that's my feeble at French, a language I can't speak
I think your suggestion *was* essentially a solution to my problem. It
merely need the tweak I mentioned, as follows.
As I read it (in the section entitled "str.chop => new_str"), the web page
your referenced indicates that chop is intended to remove *any* last
character; it merely has special semantics for record separators to
transcend the divergence of Windows from Unix.
In your case I'd prefer
s.sub!( /\\\\\z/, '\\' )
I assume "z" is a typo for "$" and I further assume that using pattern
matching is much less efficient than directly "chopping" (though the
difference is imperceptible for the amount of data usually processed by
scripting languages.)
I apologize for belaboring this point. Again, I'm in your debt for your
thoughtful responses.
Best wishes,
Richard
···
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.713 / Virus Database: 469 - Release Date: 7/8/2004
I assume "z" is a typo for "$" and I further assume that using pattern
matching is much less efficient than directly "chopping" (though the
difference is imperceptible for the amount of data usually processed by
scripting languages.)
\z is the very end of a String. It will always match after the last character of a String.
$ is the end of a line. It will match before any \n character.
There's also \Z which is the end of a String. It will match if you are at the end of the String or before a \n that is at the end of a String.
Richard Lionheart wrote:
> Hi Robert,
>
>>>My goal was to chop a final backslash it was immediately preceded
>>>by another backslash.
>>
>>Ok, then my suggestion was not a solution to your problem.
>
> Au contraire ... that's my feeble at French, a language I can't speak
> I think your suggestion *was* essentially a solution to my problem. It
> merely need the tweak I mentioned, as follows.
>
>>> So I'll use:
>>>
>>>s.chop! if /\\\\$/ =~ s
>>
>>Personally I would not use chop! since it is rather meant to remove record
>>separators (similar to chomp! but not as sophisticated, see here:
>>http://www.ruby-doc.org/docs/rdoc/1.9/classes/String.html#M001293
>
> As I read it (in the section entitled "str.chop => new_str"), the web page
> your referenced indicates that chop is intended to remove *any* last
> character; it merely has special semantics for record separators to
> transcend the divergence of Windows from Unix.
Well, yes. Technically that's true. Maybe I made the fault to put it
into the same bag with chomp, which is what I usually use when reading
files.
>>In your case I'd prefer
>>
>>s.sub!( /\\\\\z/, '\\' )
>
> I assume "z" is a typo for "$" and I further assume that using pattern
> matching is much less efficient than directly "chopping" (though the
> difference is imperceptible for the amount of data usually processed by
> scripting languages.)
Well, if you really want to know it benchmark the two vs. each other and
see which one is faster. When it comes to performance, intuition is
often wrong. Measuring is the only way to get reliable results.
> I apologize for belaboring this point. Again, I'm in your debt for your
> thoughtful responses.
Again I squeezed out a little more education through NG posts! I heartily
thank both of you for the time and effort you took on my behalf and the
additional guidance you have generously provided me. I hope others have
benefited as much as I have from this thread.
Warmest regards,
Richard
···
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.713 / Virus Database: 469 - Release Date: 7/8/2004