# Re: String interpretation

**URL:** <https://www.ruby-forum.com/t/re-string-interpretation/94222>\
**Category:** Ruby\
**Created:** [April 25, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/re-string-interpretation/94222 "2006-04-25T21:48:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [April 25, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/re-string-interpretation/94222/1 "2006-04-25T21:48:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jamal86](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ebca7d/32.png) [@jamal86](https://www.ruby-forum.com/u/jamal86)\
**Post date:** [April 25, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/re-string-interpretation/94222/2 "2006-04-25T22:38:22Z")

</div>

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
