Dear list,
I want to create a method that creates certain kinds of classes for me.
The following doesn't work:
### start pseudo code
module My_Module
class Bigclass
attr_accessor :test
def Bigclass.run(para)
@test = para
puts "your parameter is " + @test
end
def Bigclass.check
puts "I'm here"
end
end
def My_Module.subclass(name, &code)
eval <<-EOS
class #{name} < Bigclass
def #{name}.run(p)
super(p)
#{code.call}
end
end
EOS
end
end
My_Module::subclass("Newclass"){
puts "doesn't work" + @test.to_s
check()
}
My_Module::Newclass.run("my parameter")
### end pseudo code
the problem is that the #{code.call} is executed before
the class is defined, so @test isn't filled yet (super(p)
isn't executed at this time.
how can I change the code, so that »code« is part of the definition
of the class (i.e. part of its method »run«)?
(the code can be very long, so putting it in a string is
not very comfortable regarding quoting etc. (I also want
the code to be highlighted in an editor).
benny