newb: Class Method determining it's actual class?

A

Art Gillespie

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 =3D IFFFile.open("media.iff", "r")

f.class =3D> "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.

Art
 
A

Art Gillespie

Hi again,

There must be some law somewhere says that the chances of finding the
answer yourself goes up exponentially in the moments after posting to
a mailing list.

I hadn't even thought to try 'self' in a class method. Much to learn.

Thanks again,

Art
 
M

m4dc4p

It's like lighting a cigarette while waiting for the bus. Nothing
guarantees faster arrival, haha!
 
J

James Britt

Art said:
Hi again,

There must be some law somewhere says that the chances of finding the
answer yourself goes up exponentially in the moments after posting to
a mailing list.


The path to enlightenment, grasshopper, is through the 'send' button.

James

--

http://www.ruby-doc.org - The Ruby Documentation Site
http://www.rubyxml.com - News, Articles, and Listings for Ruby & XML
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
 
R

Robert Klemme

Art Gillespie said:
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
=> 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<<self:

class A
class <<self
def open
new
end
end
end

Kind regards

robert
 
R

Robert Klemme

Art Gillespie said:
Hi again,

There must be some law somewhere says that the chances of finding the
answer yourself goes up exponentially in the moments after posting to
a mailing list.

There must be a similar law that determines that you find the posting with
the answer just after you sent your own... :)

robert
 

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

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top