Adding data to text file variables

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.

Thanks in advance.

On Thu, May 7, 2009 at 4:57 PM, Stuart C.
[email protected] wrote:

predefined text file containing some data and three variables has each
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.

Thanks for your prompt reply. Can I run over a few bits of the code you
send across?

template = ERB.new(File.read(“template.erb”), nil, “%<>”)

What does the above line of code, do? What is the erb file?

File.open(“result.txt”, “w”) do |f|
f.puts template.result(binding)
end

This makes no reference to the variables, so how are they written to the
text file?

Many thanks

Jesús Gabriel y Galán wrote:

On Thu, May 7, 2009 at 4:57 PM, Stuart C.
[email protected] wrote:

predefined text file containing some data and three variables has each
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.

On Fri, May 8, 2009 at 3:58 PM, Stuart C.
[email protected] wrote:

Thanks for your prompt reply. Can I run over a few bits of the code you
send across?

template = ERB.new(File.read(“template.erb”), nil, “%<>”)

What does the above line of code, do? What is the erb file?

Reads the file template.erb, and creates an ERB template.
The file can be something like I wrote in the first email:

blah, blah
<%= variableA %>
xxxx
<%= variableB %>
yyy

File.open(“result.txt”, “w”) do |f|
f.puts template.result(binding)
end

This makes no reference to the variables, so how are they written to the
text file?

ERB receives a binding to evaluate the template. In this case we are
calling
the binding method in the same scope as the local variables, meaning
that
any code evaluated with that binding can use them.

Jesus.

Stuart C. wrote:

Thanks for your prompt reply. Can I run over a few bits of the code you
send across?

Here’s a simpler example to start with:

require ‘erb’

file_contents =<<ENDOFSTRING
Hello there <%= name %>. I see
that you are wearing a new <%= clothing %>.
That looks good on you. What a <%= adj %>
day it is today. Goodbye.
ENDOFSTRING

template = ERB.new(file_contents)
name, clothing, adj = “Diane”, “dress”, “lovely”

new_contents = template.result
puts new_contents

–output:–
Hello there Diane. I see
that you are wearing a new dress.
That looks good on you. What a lovely
day it is today. Goodbye.

The result method looks for the variables name, clothing, and adj in the
current scope.

7stud – wrote:

template = ERB.new(file_contents)
name, clothing, adj = “Diane”, “dress”, “lovely”

new_contents = template.result
puts new_contents

–output:–

The result method looks for the variables name, clothing, and adj in the
current scope.

Uhmm…not quite. The result method actually looks for the name,
clothing, and adj variables in the “toplevel” scope, i.e. the global
scope. Look at this example:

require ‘erb’

def replace(str)
name, clothing, adj = “Joe”, “hat”, “chilly”
template = ERB.new(str)

new_contents = template.result
end

name, clothing, adj = “Diane”, “dress”, “lovely”

file_contents =<<ENDOFSTRING
Hello there <%= name %>. I see
that you are wearing a new <%= clothing %>.
That looks good on you. What a <%= adj %>
day it is today. Goodbye.
ENDOFSTRING

puts replace(file_contents)

–output:–
Hello there Diane. I see
that you are wearing a new dress.
That looks good on you. What a lovely
day it is today. Goodbye.

The result method did not see the the local variables name, clothing,
adj because by default it is programmed to look in the toplevel scope.

However, result takes an argument that specifies what scope result
should look in for the values of the variables specified in the
template. And the Kernel method “binding” can be called to return the
surrounding scope. As a result, you can do this:

require ‘erb’

def replace(str)
name, clothing, adj = “Joe”, “hat”, “chilly”
template = ERB.new(str)

new_contents = template.result(binding)
end

name, clothing, adj = “Diane”, “dress”, “lovely”

file_contents =<<ENDOFSTRING
Hello there <%= name %>. I see
that you are wearing a new <%= clothing %>.
That looks good on you. What a <%= adj %>
day it is today. Goodbye.
ENDOFSTRING

puts replace(file_contents)

–output:–
Hello there Joe. I see
that you are wearing a new hat.
That looks good on you. What a chilly
day it is today. Goodbye.