[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Dynamically creating ruby source files

Matt Gretton

5/19/2007 3:59:00 PM

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.

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

6 Answers

Alex Young

5/19/2007 4:07:00 PM

0

Matt Gretton wrote:
<snip>
> 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).

--
Alex

Pau Garcia i Quiles

5/19/2007 4:29:00 PM

0

Hello,

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

Most of the code is autogenerated.

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


Quoting Matt Gretton <matthew.gretton@gmail.com>:

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



Logan Capaldo

5/21/2007 1:29:00 PM

0

On 5/19/07, Matt Gretton <matthew.gretton@gmail.com> wrote:
> 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.
>

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

> Thanks in advance.
>
> Matt.
>
> --
> Posted via http://www.ruby-....
>
>

ara.t.howard

5/21/2007 2:06:00 PM

0


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

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

there is an entire book on the topic

http://www.manning.com/h...

kind regards.

-a
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Rick DeNatale

5/21/2007 5:24:00 PM

0

On 5/21/07, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> On May 19, 2007, at 9:58 AM, Matt Gretton 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
>
> http://www.manning.com/h...
>

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

Matt Gretton

5/22/2007 3:54:00 PM

0

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.

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