From: Ross Bamford Date: 2005-11-28T18:07:29+09:00 Subject: Re: string substitution question On Mon, 28 Nov 2005 04:28:11 -0000, wrote: > I'm reading a file using: > > " str = IO.read("textfile1.txt") " > > and trying to output the results using > > " puts str " > The problem is that I want to substitute values in the file. It > includes this text: #{(3+4).to_s} > > I expected the puts statement to output 7, but it outputs the code > literally as #{(3+4).to_s} > > How can i get this substitution to happen? > > thanks for your help. > (Caveat: relatively new to Ruby) I think it's more trouble than it's worth, though I don't doubt it could be done with some insane escaping and eval. I'd have to guess that the #{} expansion is done somewhere in the parser (?). I recently made use of ERB (included with 1.8.3, don't know about other versions) for this kind of thing, which worked a treat. Your file would contain something like: <%= 3+4.to_s %> (P.s. I know it looks a little bit /.*ML/ but it's not specifically - it's just the way ERB separates the code. -- Ross Bamford - rosco@roscopeco.remove.co.uk