From: "Jesús Gabriel y Galán" Date: 2009-05-08T00:40:44+09:00 Subject: Re: Adding data to text file variables On Thu, May 7, 2009 at 4:57 PM, Stuart Clarke wrote: > I have a quick question and I apologise for my ignorance if it is easy > to perform. > > I have a GUI class which takes 3 peices of information from the user in > text fields which are loaded into 3 variables. > > I then want to click a 'Run' button and place the information from the > variables at three defined points in a text file. > > Basically a user enters some information to the GUI, clicks RUN and a > predefined text file containing some data and three variables has each > of the variables updated to reflect user input? > > First, is this possible? If so, where do I start? > > I was thinking put three variables in the text file: > > @variableA > @variableB > @variableC > > I these will updated (like find and replace) using the Ruby GUI. This sounds like templating. Take a look at some templating engines like ERB. You can have a file like this: blah, blah <%= variableA %> xxxx <%= variableB %> yyy and your program can do: require 'erb' variableA = "something" variableB = "other thing" template = ERB.new(File.read("template.erb"), nil, "%<>") File.open("result.txt", "w") do |f| f.puts template.result(binding) end If you just need simple search and replace, maybe a simpler approach is enough, but I like the power of templating like this. Hope this helps, Jesus.