# Parsing text files with dynamic contents

**URL:** <https://www.ruby-forum.com/t/parsing-text-files-with-dynamic-contents/84161>\
**Category:** Ruby\
**Created:** [February 1, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/parsing-text-files-with-dynamic-contents/84161 "2007-02-01T22:36:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![martignoni-a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@martignoni-a](https://www.ruby-forum.com/u/martignoni-a)\
**Post date:** [February 1, 2007, 10:36pm UTC](https://www.ruby-forum.com/t/parsing-text-files-with-dynamic-contents/84161/1 "2007-02-01T22:36:17Z")

</div>

Let’s guess a simple example: i need to parse a text file, wityh  
variables  
inside, and then output again the resulting text to a file or to screen.

-------------start example text------------

hello, it is #{now}, your name is #{name}

-------------end example text------------

variables could be written also $var, or whatever else could be better.

but obviously when i do

file.each do |line|  
puts line #here i need to do something to find the variable and  
output the correct value  
end

How would you ruby experts do the correct parsing to output values  
instead  
of the text content? Is there any class available that do this?

Thank you very much for helping

–

Andrea M.

[http://www.superandrew.it](http://www.superandrew.it)

“The real problem is not whether machines think but whether men do.”  
(B. F. Skinner )

---

<div class="post-metadata">

**Author:** ![martignoni-a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@martignoni-a](https://www.ruby-forum.com/u/martignoni-a)\
**Post date:** [February 1, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/parsing-text-files-with-dynamic-contents/84161/2 "2007-02-01T23:29:24Z")

</div>

On Fri, Feb 02, 2007 at 06:35:21AM +0900, Andrea M. wrote:

> variables could be written also $var, or whatever else could be better.
> 
> but obviously when i do
> 
> file.each do |line|  
> puts line #here i need to do something to find the variable and  
> output the correct value  
> end
> 
> How would you ruby experts do the correct parsing to output values instead  
> of the text content? Is there any class available that do this?

Google for ‘erb’ and ‘eruby’

---

<div class="post-metadata">

**Author:** ![martignoni-a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@martignoni-a](https://www.ruby-forum.com/u/martignoni-a)\
**Post date:** [February 2, 2007, 12:12am UTC](https://www.ruby-forum.com/t/parsing-text-files-with-dynamic-contents/84161/3 "2007-02-02T00:12:31Z")

</div>

Well, that seems to be exactly what i was looking for, thank you Brian.  
So i  
think i could give erb every line to parse or make him parse the entire  
file  
before output it on the screen, right?  
now, i’ve got a little question more.

Let’s say i have a class, Foo, who calls a method of another class to  
print  
the output. For example

class Foo

@w  
@myvar

def initialize  
w=Writer.new  
end

def print\_something  
w.put\_on\_screen(1)  
end  
end

class Writer  
def put\_on\_screen(number)  
file=File.open(“./screens/screen#{number}”,“r”)  
file.each do |line|  
e=ERB.new(line)  
puts e.result  
end  
end  
end

in the txt file, how can i make visible @myvar to print it on the  
screen?

Thanks again

2007/2/1, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > How would you ruby experts do the correct parsing to output values  
> > instead  
> > of the text content? Is there any class available that do this?
> 
> Google for ‘erb’ and ‘eruby’

–

Andrea M.

cell. +39 333 2672629  
email: removed\_email\_address@domain.invalid  
[http://www.superandrew.it](http://www.superandrew.it)

“The real problem is not whether machines think but whether men do.”  
(B. F. Skinner )

---

<div class="post-metadata">

**Author:** ![martignoni-a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@martignoni-a](https://www.ruby-forum.com/u/martignoni-a)\
**Post date:** [February 2, 2007, 10:06am UTC](https://www.ruby-forum.com/t/parsing-text-files-with-dynamic-contents/84161/4 "2007-02-02T10:06:05Z")

</div>

2007/2/2, Jan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> print
> 
> > end  
> > file=File.open(“./screens/screen#{number}”,“r”)
> 
> ERB#result has another parameter, where you can pass binding - the  
> vars in the file will be evaluated in that context (see  
> [http://ruby-doc.org/core/classes/ERB.html#M002835](http://ruby-doc.org/core/classes/ERB.html#M002835))

wow, that’s great! I didn’t understand very well how the method binding  
passes that instance variables, it seems that passing a binding object  
is  
like passing an array of instance variable of the object for which it  
is  
called?

I’ve tried to look up in the docs, but i didn’ find a clear explanation.

Thanks a lot

–

Andrea M.

[http://www.superandrew.it](http://www.superandrew.it)

“The real problem is not whether machines think but whether men do.”  
(B. F. Skinner )

---

<div class="post-metadata">

**Author:** ![martignoni-a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/8491ac/32.png) [@martignoni-a](https://www.ruby-forum.com/u/martignoni-a)\
**Post date:** [February 2, 2007, 12:35am UTC](https://www.ruby-forum.com/t/parsing-text-files-with-dynamic-contents/84161/5 "2007-02-02T00:35:34Z")

</div>

On 2/2/07, Andrea M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @w  
> @myvar
> 
> def initialize  
> w=Writer.new  
> end
> 
> def print\_something

- w.put\_on\_screen(1)

- 

```
w.put_on_screen(1, binding)

```

> end  
> end
> 
> class Writer

- def put\_on\_screen(number)

- def put\_on\_screen(number, b=TOPLEVEL\_BINDING)

> ```
> file=File.open("./screens/screen#{number}","r")
> file.each do |line|
> e=ERB.new(line)
> 
> ```

- 

```
          puts e.result

```

- 

```
         puts e.result(b)

```

> ```
> end
> 
> ```
> 
> end  
> end
> 
> in the txt file, how can i make visible @myvar to print it on the screen?

ERB#result has another parameter, where you can pass binding - the  
vars in the file will be evaluated in that context (see  
[http://ruby-doc.org/core/classes/ERB.html#M002835](http://ruby-doc.org/core/classes/ERB.html#M002835))
