Running a class from the base class


(tim) #1

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


(Shashank Date) #2

“tim” tim.wade@nautronix.com.au wrote in message

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


(shasckaw) #3

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

tim wrote:

···

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


(tim) #4

Shashank Date wrote:

“tim” tim.wade@nautronix.com.au wrote in message

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

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…