[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Adding data to text file variables

Stuart Clarke

5/7/2009 2:57:00 PM

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.
--
Posted via http://www.ruby-....

5 Answers

Jesús Gabriel y Galán

5/7/2009 3:41:00 PM

0

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.

Stuart Clarke

5/8/2009 1:58:00 PM

0

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 Clarke
> <stuart.clarke1986@gmail.com> 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.

--
Posted via http://www.ruby-....

Jesús Gabriel y Galán

5/10/2009 7:49:00 PM

0

On Fri, May 8, 2009 at 3:58 PM, Stuart Clarke
<stuart.clarke1986@gmail.com> wrote:
> Thanks for your prompt reply. Can I run over a few bits of the code you
> send across?
>
>> template =3D 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
>> <%=3D variableA %>
>> xxxx
>> <%=3D variableB %>
>> yyy
>>

> > File.open("result.txt", "w") do |f|
>> =A0 =A0f.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 callin=
g
the binding method in the same scope as the local variables, meaning that
any code evaluated with that binding can use them.

Jesus.

7stud --

5/10/2009 11:38:00 PM

0

Stuart Clarke 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.
--
Posted via http://www.ruby-....

7stud --

5/10/2009 11:56:00 PM

0

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.


--
Posted via http://www.ruby-....