Class Methods and derivation

D

Damphyr

I do something like the following

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.-
--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
T

ts

D> I do something like the following

Try something like this

moulon% cat b.rb
#!/usr/bin/ruby
class Test
def self.open
tt = new
yield tt
ensure
tt.close if tt
end

def close
end
end

module SomeModule
def some_module_method
puts "some_module_method"
end
end

class DerivedTest<Test
include SomeModule
end

DerivedTest.open{|tt|
tt.some_module_method
}
moulon%

moulon% ./b.rb
some_module_method
moulon%



Guy Decoux
 
R

Robert Klemme

ts said:
Try something like this

moulon% cat b.rb
#!/usr/bin/ruby
class Test
def self.open
tt = new
yield tt
ensure
tt.close if tt
end

def close
end
end

module SomeModule
def some_module_method
puts "some_module_method"
end
end

class DerivedTest<Test
include SomeModule
end

DerivedTest.open{|tt|
tt.some_module_method
}
moulon%

moulon% ./b.rb
some_module_method
moulon%



Guy Decoux

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
 
D

Damphyr

Robert said:
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.-
--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
R

Robert Klemme

Damphyr said:
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.

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.

Kind regards

robert
 
D

Damphyr

Robert said:
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 :)
V.-

--
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
D

Damphyr

Robert said:
Damphyr wrote:
=20
<sonorous voice>don't drink and code</sonorous voice>
The thing is, I woke up today and the first thing I thought was: "&=A7$&!=
=20
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=20
catch it later (there's always a second chance). No way to avoid public=20
humiliation if you post though :)
V.-
--=20
http://www.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - =E4=F9=F1=E5=DC=ED =F5=F0=E7=F1=E5=F3=DF=E1 =E7=EB=
=E5=EA=F4=F1=EF=ED=E9=EA=EF=FD =F4=E1=F7=F5=E4=F1=EF=EC=E5=DF=EF=F5.
http://www.freemail.gr - free email service for the Greek-speaking.
 
D

Devin Mullins

Robert said:
<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.

Devin
 
G

Gavin Kistner

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.

Not that I have any experience with that.
 
M

Martin DeMello

Gavin Kistner said:
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.

Obviously they were approximations to chunky_bacon :)

martin
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top