Logan Capaldo ha escrito:
How about:
new_string = ""
string.scan(/\0/) do |m|
new_string = "%o" % $~.offset(0).first
end
That should work okay (well, it should be new_string << ..., thou).
I'll admit I was unaware of MatchData's offset method. That's cool.
Using string.scan() should be only slightly slower than the
while/index() loop do to the inherent yield in scan {}.
If you are comming from Perl, I would also recommend to stick with
Regexp.last_match instead of $~ for clarity. I did 5+ years of Perl
but I'll be damned if I remember what all the $symbol vars stand for
without looking them up.