In article <20011120111915.L58741-100000 / www.stelesys.com>,
Jim Freeze  <jim / freeze.org> wrote:
>Hi
>Here I am again.
>
>I have a file with the line '#{a}' in it.
>I would like the variable 'a' to be evaluated
>when printed.
>
>cat t
>#{a}
>irb(main):001:0> a=5
>5
>irb(main):002:0> l=IO.readlines("t")
>["#{a}\n"]
>irb(main):003:0> puts l[0]
>#{a}
>nil
>irb(main):004:0> puts "#{a}"
>5
>
>
>How would I go about getting the #{a} to evaluate here.
>I'm trying to find a nice way for ruby to substitute
>values into variables without using regex.


It won't evaluate when read from the file because that string is read in 
as though it were a string defined like:

str = '#{a}'

No interpolation is done.

The way I did this in the Ruby Templating System was to define an 
interpolate method on String:

class String
  def interpolate(bind)
    eval( '"'+self.gsub(/\\/,'\&\&').gsub(/"/,'\"')+'"',bind)
  end
end

The extra gsub's here escapes "'s and /'s so that your string can contain 
something like: print "\n" 
Of course, if you do it this way, you have to pass in the current binding
as well. ( str.interpolate(binding) )

BTW: You might want to take a look at my Ruby Templating System.  It 
allows you to create template files which contain embedded Ruby code.  The 
template file(s) are read in and the values of the Ruby code are 
substituted within the File String which can then be written out to 
another file if you wish.  That kind of sounds like what you're trying to 
do.

See:
http://www.aracnet.com/~ptkwt


Phil