Forum: Ruby Adding data to text file variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2009-05-07 16:57
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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-05-07 17:41
(Received via mailing list)
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.
Cf25fbf53c67e27d95845e77e949b56f?d=identicon&s=25 Stuart Clarke (sclarke)
on 2009-05-08 15:58
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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-05-10 21:53
(Received via mailing list)
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 = 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.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-05-11 01:38
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.
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-05-11 01:55
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.
This topic is locked and can not be replied to.