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
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
···
On 10/7/05, Art Gillespie <agillesp@gmail.com> 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
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<<self:
It's like lighting a cigarette while waiting for the bus. Nothing
guarantees faster arrival, haha!
Art Gillespie wrote:
···
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
On 10/7/05, Art Gillespie <agillesp@gmail.com> 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.
>
> Art
>
>
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.
There must be a similar law that determines that you find the posting with the answer just after you sent your own...
robert
···
Art Gillespie <agillesp@gmail.com> wrote:
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.