# Adding data to text file variables

**URL:** <https://www.ruby-forum.com/t/adding-data-to-text-file-variables/167414>\
**Category:** Ruby\
**Created:** [May 7, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/adding-data-to-text-file-variables/167414 "2009-05-07T16:57:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 7, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/adding-data-to-text-file-variables/167414/1 "2009-05-07T16:57:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 7, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/adding-data-to-text-file-variables/167414/2 "2009-05-07T17:41:23Z")

</div>

On Thu, May 7, 2009 at 4:57 PM, Stuart C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 8, 2009, 3:58pm UTC](https://www.ruby-forum.com/t/adding-data-to-text-file-variables/167414/3 "2009-05-08T15:58:17Z")

</div>

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.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 10, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/adding-data-to-text-file-variables/167414/4 "2009-05-10T21:53:31Z")

</div>

On Fri, May 8, 2009 at 3:58 PM, Stuart C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 11, 2009, 1:38am UTC](https://www.ruby-forum.com/t/adding-data-to-text-file-variables/167414/5 "2009-05-11T01:38:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sclarke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@sclarke](https://www.ruby-forum.com/u/sclarke)\
**Post date:** [May 11, 2009, 1:55am UTC](https://www.ruby-forum.com/t/adding-data-to-text-file-variables/167414/6 "2009-05-11T01:55:36Z")

</div>

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.
