running a class from the base class

T

tim

Hi

I have examined the test object from the test unit stuff and i cannot
work out how the base class executes the main test object... i want to
efectively do the same thing. Can anybody give me some tips on how to do
this. Effectively i want to:

class SpecificArchitecture << BaseArchitecture
def main(args)
....
end
end

And when the file is run it just figures out how to make the object and
call into main. I currently have a number of derived classes that all
have something like:

begin
xx = SpecificArchitecture.new()
xx.main(ARGV)
end

at the end. Which i would like to remove.

thanks
Tim
 
S

Shashank Date

tim said:
I currently have a number of derived classes that all
have something like:

begin
xx = SpecificArchitecture.new()
xx.main(ARGV)
end

at the end. Which i would like to remove.
^^^^^^^^
If you always want to do this _at the end_ of your program,
then take a look at the Kernel#at_exit {} method.

-- shanko
 
S

shasckaw

It is hard to understand what you really want, but I'll try to help.

Perhaps you just need to use some meta-class programming:

class SpecificArchitecture < BaseArchitecture
def SpecificArchitecture.main(args)
...
end
end

And then you just do:
begin
SpecificArchitecture.main(ARGV)
end

It means that you call the main function without instanciation.

I think UnitTest do it another way, but I currently don't know how.

Lio
 
T

tim

Shashank said:
^^^^^^^^
If you always want to do this _at the end_ of your program,
then take a look at the Kernel#at_exit {} method.

-- shanko
thanks -- yes after more spelunking through the test stuff we now have
something like this:

============ file 1 ===========

require 'y'

class X < Y
def blah
print 'in blah'
end
end

=========== file 2 ============

class Y
def self.run()
::ObjectSpace::each_object(Class) do |class_object|
# we only want classes that are above this one...
if (Y > class_object)
instance = class_object.new()
instance.blah()
end
end
end

at_exit{ exit(Y.run()) }

==================================

I am sure that there are some subtly gothca's in this but it does mean
that all of my X classes now don't have to have:

begin
instance = X.new()
instance.blah()
end

on them all. Theses are obviously much cut down examples -- there is a
lot more cruft in all real implementations...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top