From: vjoel@... Date: 2001-10-28T11:55:59+09:00 Subject: [ruby-talk:23659] Re: How to get a String to interpolate itself? Phil Tomson wrote: > > I'm trying to read lines from a file that might contain something like: > > Here is a line with a variable to interpolate: #{var} > > Now when I read it in like: > > var = 99 > File.foreach("junk.rbt") {|line| > puts line > } > > of course I get the same line in the file with the #{var} in it. > I'd like to get: > Here is a line with a variable to interpolate: 99 > > I suspect there must be a way to do this since using "" causes > interpolation, how can I get an existing String to interpolate itself? > > Phil How's this? var = 3 str = 'This is the value of var: #{var}' puts eval "\"#{str}\"" # ==> This is the value of var: 3 # also: puts eval '"' + str + '"' # ==> This is the value of var: 3 -- Joel VanderWerf California PATH, UC Berkeley mailto:vjoel@path.berkeley.edu Ph. (510) 231-9446 http://www.path.berkeley.edu FAX (510) 231-9512