# Dynamically creating ruby source files

**URL:** <https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566>\
**Category:** Ruby\
**Created:** [May 19, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566 "2007-05-19T17:58:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [May 19, 2007, 5:58pm UTC](https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566/1 "2007-05-19T17:58:57Z")

</div>

Hello all,

What techniques do people generally use to create files and write ruby  
code to them?

One way of acheving this (a horrible way) would be just to write the  
correct code to a file as follows. (This just creates a file called  
file\_test.rb that defines a module with two methods)

passed\_in\_name = “module1”  
passed\_in\_methods = [“meth1”,“meth2”]

File.open(“test\_file.rb”,“w”) do |file|  
file.puts “module #{passed\_in\_name}”  
file.puts

passed\_in\_methods.each do |meth|  
file.puts “\tdef #{meth}”  
file.puts “\t\t#insert method code here”  
file.puts “\tend”  
file.puts  
end

file.puts “end”  
end

I feel there must exist a more elegant way of creating a file liek this.  
For example, I have heard that ruby on rails dynamically creates files  
containing ruby code. This behavior seems pretty common to various ruby  
projects, for example I know rails creates ruby source code dynamically.  
Not having had experience with such projects I have tried in vain to  
search google for information.

Any advice or generally a point the right direction (to other resources  
etc) would be greatly appreciated.

Thanks in advance.

Matt.

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [May 19, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566/2 "2007-05-19T18:06:56Z")

</div>

Matt G. wrote:

> I feel there must exist a more elegant way of creating a file liek this.  
> For example, I have heard that ruby on rails dynamically creates files  
> containing ruby code. This behavior seems pretty common to various ruby  
> projects, for example I know rails creates ruby source code dynamically.  
> Not having had experience with such projects I have tried in vain to  
> search google for information.

As far as I remember, Rails just uses ERB templates. Alternatively,  
there’s ruby2ruby. I’ve used the former, but not the latter (yet).

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [May 19, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566/3 "2007-05-19T18:29:59Z")

</div>

Hello,

You might want to look at the source of XSPF for Ruby:  
[http://xspf.rubyforge.org](http://xspf.rubyforge.org)

Most of the code is autogenerated.

–  
Pau Garcia i Quiles  
[http://www.elpauer.org](http://www.elpauer.org)  
(Due to the amount of work, I usually need 10 days to answer)

Quoting Matt G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [May 21, 2007, 3:31pm UTC](https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566/4 "2007-05-21T15:31:04Z")

</div>

On 5/19/07, Matt G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> passed\_in\_name = “module1”  
> file.puts  
> search google for information.
> 
> Any advice or generally a point the right direction (to other resources  
> etc) would be greatly appreciated.

I don’t know that you necessarily want to create a file full of ruby  
source (you might) but alternatively you can use ruby’s  
meta-programming facilities:

passed\_in\_name = “Module1”  
passed\_in\_methods = [“meth1”, “meth2”]

mod = Module.new  
Object.const\_set(passed\_in\_name, mod)

mod.module\_eval do  
passed\_in\_methods.each do |m|  
define\_method(m) do |arg1, arg2, …, argn|  
body\_of\_method  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [May 21, 2007, 4:07pm UTC](https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566/5 "2007-05-21T16:07:10Z")

</div>

On May 19, 2007, at 9:58 AM, Matt G. wrote:

> passed\_in\_name = “module1”  
> file.puts  
> projects, for example I know rails creates ruby source code  
> Matt.

there is an entire book on the topic

[Code Generation in Action](http://www.manning.com/herrington/)

kind regards.

-a

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [May 22, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566/6 "2007-05-22T17:53:46Z")

</div>

Hello all,

Thank you to all who have replied to my post so far. It is very much  
appreciated.

When I posted my initial question I had a specific goal in mind.

That is the ability to automatically create code templates with relevant  
includes/requires/example classes/modules/methods.

I also wanted to use the technique to create test cases specific to the  
code project I am working on.

In the end I have written (well almost it’s almost finished…) a class  
to assist with the dynamic creation of files containing ruby code.

It models the code structure as a tree to make the construction of the  
code as straightforward as possible (you can add method nodes to your  
module nodes etc…). It is then easy to convert the tree to a string  
and write the string to a file!

I’m happy to post the finished class if anyone is interested but in all  
honesty it’s quite basic. Still, when it is finished it’ll serve my  
purposes perfectly!

Thanks again,

Matt.

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [May 21, 2007, 7:24pm UTC](https://www.ruby-forum.com/t/dynamically-creating-ruby-source-files/104566/7 "2007-05-21T19:24:37Z")

</div>

On 5/21/07, ara.t.howard [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On May 19, 2007, at 9:58 AM, Matt G. wrote:
> 
> > Hello all,
> > 
> > What techniques do people generally use to create files and write ruby  
> > code to them?

…

> > For example, I have heard that ruby on rails dynamically creates files  
> > containing ruby code. This behavior seems pretty common to various  
> > ruby  
> > projects, for example I know rails creates ruby source code  
> > dynamically.

> there is an entire book on the topic
> 
> [Code Generation in Action](http://www.manning.com/herrington/)

In my experience, it’s rare to approach this in Ruby by writing ruby  
code to files.

Rails uses metaprogramming techniques to dynamically generate and  
modify classes at run-time, no need to do this by writing and reading  
source files.

It also uses other techiques to generate other artifacts like html,  
such as using eruby which allows embedded ruby code to produce part or  
all of the content dynamically again at runtime.

This is how I’ve usually seen things like this done in Ruby.

I’m not familiar with the book Ara recommended, but a quick scan of  
the on-line contents and sample chapter seems to indicate that it’s  
really about generating code for languages other than Ruby, although  
it does seem to use Ruby as the language for writing code generators.

It seems to me that the dynamic runtime generation/expansion allowed  
by a dynamic language like Ruby provides the advantages of code  
generation and avoids drawbacks like how to approach re-generating  
modified code, since the generated code never exists as a file to be  
modified.

I’d recommend that the OP do a google search on ruby metaprogramming.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
