Forum: Ruby Dynamically creating ruby source files

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.
E271235505e62d906b6e5d618aee43ca?d=identicon&s=25 Matt Gretton (mattg)
on 2007-05-19 17:58
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.
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-05-19 18:06
(Received via mailing list)
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).
5acffb3fde70f28b9eab13eb78a25e63?d=identicon&s=25 Pau Garcia i Quiles (Guest)
on 2007-05-19 18:29
(Received via mailing list)
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 Gretton <matthew.gretton@gmail.com>:
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2007-05-21 15:31
(Received via mailing list)
On 5/19/07, Matt Gretton <matthew.gretton@gmail.com> 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
1bac2e65d64faf472cf2ebc94f0f5ee0?d=identicon&s=25 ara.t.howard (Guest)
on 2007-05-21 16:07
(Received via mailing list)
On May 19, 2007, at 9:58 AM, Matt Gretton 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

   http://www.manning.com/herrington/

kind regards.

-a
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2007-05-21 19:24
(Received via mailing list)
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/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/
E271235505e62d906b6e5d618aee43ca?d=identicon&s=25 Matt Gretton (mattg)
on 2007-05-22 17:53
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.
This topic is locked and can not be replied to.