Re: String interpretation

The eval technique works for now–thanks! Is there also a way I can
reverse the conversion, i.e., getting a string with standard (not
RegExp) tokens from a string variable that has the actual characters,
themselves?

Jamal

On Wed, 26 Apr 2006, Jamal M. wrote:

The eval technique works for now–thanks! Is there also a way I can
reverse the conversion, i.e., getting a string with standard (not
RegExp) tokens from a string variable that has the actual characters,
themselves?

with each and every feature creep you’ll be wanting something like this:

harp:~ > cat a.rb
class SearchExpression < ::String
INTERPOLATE = {
‘\n’ => “\n”,
‘\t’ => “\t”,
‘\r’ => “\r”,
‘\f’ => “\f”,
}
def initialize *a, &b
super and interpolate!
end
def interpolate!
INTERPOLATE.each do |k,v|
gsub! Regexp::new(Regexp::escape(k)), v
end
end
def uninterpolate!
INTERPOLATE.invert.each do |k,v|
gsub! Regexp::new(Regexp::escape(k)), v
end
end
end

s = SearchExpression::new ‘foo\tbar\n’
p s
puts s

s.uninterpolate!
p s
puts s

harp:~ > ruby a.rb
“foo\tbar\n”
foo bar
“foo\tbar\n”
foo\tbar\n

cheers.

-a