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 <ged / FaerieMUD.org>
Rubymage, Believer, Architect
The FaerieMUD Consortium <http://www.FaerieMUD.org/>