From: Eric Bennett Date: 2001-05-01T23:32:11+09:00 Subject: [ruby-talk:14489] Re: How to do it the Ruby-way 2 On Tue, 1 May 2001, Stefan Matthias Aust wrote: > def a(pat, name) > eval "print \"#{pat}\"" > end > > But, I think, it's really ugly. I'd like to use a pattern string read > in from an external file (from the trusted source) and then emit it > using all bindings currently available in that method. How can this > be achieved? I don't know how to get rid of the eval, but you can move the print out of it and get it into a nice functional form: def expression_for_string( string ) '"' + string.gsub( /(["\\])/, '\\\\\1' ) + '"' end def with_binding_expand( name_binding, string_template ) eval expression_for_string( string_template ), name_binding end def a( string_template, name ) print with_binding_expand( binding, string_template ) end a( "My name is \#{name}\n", "fred" ) a( "My 'na\\me\" is \#{name}\n", "charles" ) a( "My quine is almost \"\#{string_template}\"\n", "Ignored" ) - Eric B. -- "Bound to please"