Dynamic creation of classes and methods

I want to create classes and methods on fly.
It will look something like that:

Program imports module Foo.
Then it:

  • calls functions from Foo
  • create objects of class Foo::Bar for any Bar
  • calls methods of Foo::Bar

1st and 3rd can be done with method_missing
But how can I create classes on fly ?

Where does this message come from and how can I overload this:
NameError: uninitialized constant Bar at Foo

Where does this message come from and how can I overload this:
NameError: uninitialized constant Bar at Foo

The constant Foo::Bar don't exist

pigeon% ruby -e 'module Foo Bar = Class.new; end; p Foo::Bar.new'
#<Foo::Bar:0x401b0aa4>
pigeon%

Guy Decoux

I mean not just for one Bar, for any Bar.

What I want is creation of constants at first request,
the same way I can create methods with method_missing.

···

On Mon, Aug 05, 2002 at 05:51:58PM +0900, ts wrote:

Where does this message come from and how can I overload this:
NameError: uninitialized constant Bar at Foo

The constant Foo::Bar don’t exist

pigeon% ruby -e ‘module Foo Bar = Class.new; end; p Foo::Bar.new’
#Foo::Bar:0x401b0aa4
pigeon%

What I want is creation of constants at first request,

Use begin ... rescue

pigeon% ruby
module Foo
end
begin
   p Foo::Bar.new
rescue NameError
   Foo.const_set("Bar", Class.new)
   retry
end
^D
#<Foo::Bar:0x401b0914>
pigeon%

and add the appropriate test to make it work cleanly.

Guy Decoux

How can I find out which constant was missing ?
Is it possible to put this code in module so program which uses the
module doesn’t have to catch anything ?

···

On Mon, Aug 05, 2002 at 07:37:50PM +0900, ts wrote:

What I want is creation of constants at first request,

Use begin … rescue

pigeon% ruby
module Foo
end
begin
p Foo::Bar.new
rescue NameError
Foo.const_set(“Bar”, Class.new)
retry
end
^D
#Foo::Bar:0x401b0914
pigeon%

and add the appropriate test to make it work cleanly.

How can I find out which constant was missing ?
Is it possible to put this code in module so program which uses the
module doesn't have to catch anything ?

I've not yet understood what you want to do. Can you give a concrete
example ?

Guy Decoux

Hello folks

Im looking to delve into Ruby and would like to know if there is a
preferred book, site, etc. I did a search on Amazon and several books were
listed. Is there a preferred one?

Also, if this info is in the FAQ please point me in the right
direction. Where can I get a hold of the FAQ’s? Thanks.

I want to make a module that has functions and classes, just like any
other module, but with exception that they created at the time of
first usage.

So user program calls Foo::Bar.new
Foo::Bar isn’t registered yet. So Ruby calls some function in Foo,
which sees that “Bar” is being requested. It checks in it’s database
what bar ought to be, creates it, and calls Foo::Bar.new, which
returns new object. Next time when Foo::Bar is called it already
exists, so nothing extraordinary needs to happen.

I think it might be good idea in case when Foo contains lot of classes
but only very small percent of them is actually used.

···

On Mon, Aug 05, 2002 at 10:57:27PM +0900, ts wrote:

How can I find out which constant was missing ?
Is it possible to put this code in module so program which uses the
module doesn’t have to catch anything ?

I’ve not yet understood what you want to do. Can you give a concrete
example ?

http://www.ruby-lang.org/en/doc.html

hth,
alia

···

Im looking to delve into Ruby and would like to know if there is a
preferred book, site, etc. I did a search on Amazon and several books
were listed. Is there a preferred one?

Also, if this info is in the FAQ please point me in the right
direction. Where can I get a hold of the FAQ’s? Thanks.

So user program calls Foo::Bar.new
Foo::Bar isn't registered yet. So Ruby calls some function in Foo,
which sees that "Bar" is being requested. It checks in it's database
what bar ought to be, creates it, and calls Foo::Bar.new, which
returns new object. Next time when Foo::Bar is called it already
exists, so nothing extraordinary needs to happen.

Except, I think, that ruby don't work like this :slight_smile:

When you write Foo::Bar, ruby first search for the constant Foo if it
don't find it it give an error. If Foo is a class or a module, then ruby
search the constant Foo::Bar or it give an error, otherwise it try to call
the method Foo::Bar()

What you can do is rather than call Foo::Bar.new() call Foo.Bar() which
(via #method_missing) will create the class Foo::Bar (if it don't exist)
and then call the method ::new (something like Kernel#Integer)

Something like this

pigeon% cat b.rb
#!/usr/bin/ruby
module Foo
   def self.method_missing(arg)
      bar = if const_defined?(arg)
               puts "retrieve #{arg}"
               const_get(arg)
            else
               puts "create #{arg}"
               const_set(arg, Class.new)
            end
      bar.new
   end
end

p Foo.Bar()
p Foo.Bar()
pigeon%

pigeon% b.rb
create Bar
#<Foo::Bar:0x401b06f8>
retrieve Bar
#<Foo::Bar:0x401b0518>
pigeon%

Guy Decoux

Mike Shoemaker wrote:

Hello folks

Im looking to delve into Ruby and would like to know if there is a
preferred book, site, etc. I did a search on Amazon and several books
were listed. Is there a preferred one?

Also, if this info is in the FAQ please point me in the right
direction. Where can I get a hold of the FAQ’s? Thanks.

To my mind, the definitive book is still the pickax book (Programming
Ruby, The Pragmatic Programmer’s Guide, by David Thomas and Andrew
Hunt). But there are many others that are useful. Still, I feel that’s
the best one to start with.

···


– Charles Hixson
Gnu software that is free,
The best is yet to be.