From: Michael Granger Date: 2002-09-22T02:52:15+09:00 Subject: Re: String[#.]interpolate (was Re: String interpolation at will?) Hal E. Fulton (in "String[#.]interpolate (was Re: String interpolation at will?)") wrote: [...] > And to answer my own post again, I find that > this works OK. [...] You can also require a Binding as an argument to #interpolate, and then do the eval with it, which allows you to interpolate from arbitrary scopes: class String def interpolate( scope ) unless scope.is_a?( Binding ) raise TypeError, "Argument to interpolate must be a Binding, not "\ "a #{scope.class.name}" end copy = self.gsub( /"/, %q:\": ) eval( '"' + copy + '"', scope ) end end # Test it out: str = <<'END' He said, "I am the #{alpha} and the #{omega}." Have a nice day. END puts str alpha = "walrus" omega = "weather is fine" puts str.interpolate( binding ) # Output: He said, "I am the #{alpha} and the #{omega}." Have a nice day. He said, "I am the walrus and the weather is fine." Have a nice day. -- Michael Granger Rubymage, Believer, Architect The FaerieMUD Consortium