class Test
def Test.open
tt=Test.new
yield tt
tt.close
end
end
class DerivedTest<Test
include SomeModule
end
Obviously the following will fail
DerivedTest.open{|tt|
tt.some_module_method
}
so I moved open to DerivedTest.
Can I write a class method that instantiates the proper derived class?
I need a way to find the class of the caller and instantiate that
Am I asking for the impossible? I think not, since IO.open does this, although the code is C - the call to rb_class_new_instance obviously instantiates the proper class. How can I do that in pure Ruby?
Cheers,
V.-
Guy, you were too fast again! So I cut out my similar example and
just add what would have been my explanation:
The trick is not to do Test.new but to use just new (calls self.new which
is DerivedTest in sub class).
Kind regards
robert
I can always rely on Guy for a fast answer :).
Simple, when not very obvious. I guess I had to keep in mind that new is private and for private methods self. is implicit.
Thanks guys,
V.-
Simple, when not very obvious. I guess I had to keep in mind that new is private and for private methods self. is implicit.
new is *not* private - initialize is. How else could you do Foo.new? new is a method of the class intance and initialize is an instance method.
Forget even that I said it. It was too late after too many beers
<sonorous voice>don't drink and code</sonorous voice>
The thing is, I woke up today and the first thing I thought was: "&§$&! that phrase about new is soooo wrong. What was I thinking?"
So the moral of the story is:
Don't drink and post.
If you code then you can always run your (ever present) unit tests and catch it later (there's always a second chance). No way to avoid public humiliation if you post though
V.-
____________________________________________________________________ http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ. http://www.freemail.gr - free email service for the Greek-speaking.
Yeah, but then you end up with a lot of variable names like 'sausage' and 'bratwurst' and 'bun', which all seemed hilarious and vaguely appropriate at the time, but which make no sense one week later.
Not that I have any experience with that.
···
On Sep 29, 2005, at 6:24 AM, Devin Mullins wrote:
Robert Klemme wrote:
<sonorous voice>don't drink and code</sonorous voice>
Seriously, drinking and coding is a great combination. It made a lot of my programming assignments in college worth doing.
Obviously they were approximations to chunky_bacon
martin
···
Gavin Kistner <gavin@refinery.com> wrote:
On Sep 29, 2005, at 6:24 AM, Devin Mullins wrote:
> Robert Klemme wrote:
>> <sonorous voice>don't drink and code</sonorous voice>
> Seriously, drinking and coding is a great combination. It made a
> lot of my programming assignments in college worth doing.
Yeah, but then you end up with a lot of variable names like 'sausage'
and 'bratwurst' and 'bun', which all seemed hilarious and vaguely
appropriate at the time, but which make no sense one week later.