Dynamically creating ruby source files

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.

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).

Hello,

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

Most of the code is autogenerated.


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

Quoting Matt G. [email protected]:

On 5/19/07, Matt G. [email protected] 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

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

kind regards.

-a

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.

On 5/21/07, ara.t.howard [email protected] 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

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/