From: Robert Klemme Date: 2005-10-08T20:16:50+09:00 Subject: Re: Class Method determining it's actual class? Art Gillespie wrote: > Hi, > > New to Ruby and to the list. I've looked through pickaxe and through > the ruby source code and haven't figured this one out. > > With the built-in File class, if I derive from it and call a class > method (e.g., open) what I get back is an instance of my derived > class: > > class IFFFile < File > end > > f = IFFFile.open("media.iff", "r") > > f.class => "IFFFile" > > How does one do this with their own class methods? > > class A > def A.open > #how do we know here if the message recipient is a subclass of A? > #i.e., can't we generalize it so that it is, in effect, class.new? > A.new > end > end > > Many thanks for any pointers to what I'm missing. class A def self.open new end end class B < A end >> A.open.class => A >> B.open.class => B Note, the crucial bit is the method body. def self.open and def A.open define the same method but using "self" makes refactoring (especially renaming of A) easier. You could as well use class<