Jesús Gabriel y Galán
5/7/2009 3:41:00 PM
On Thu, May 7, 2009 at 4:57 PM, Stuart Clarke
<stuart.clarke1986@gmail.com> 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.