# Newb: Class Method determining it's actual class?

**URL:** <https://rubytalk.org/t/newb-class-method-determining-its-actual-class/21408>\
**Category:** ruby-talk\
**Created:** [7 October 2005 20:25 UTC](https://rubytalk.org/t/newb-class-method-determining-its-actual-class/21408 "2005-10-07T20:25:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Art\_Gillespie](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@Art\_Gillespie](https://rubytalk.org/u/Art_Gillespie)\
**Post date:** [7 October 2005 20:25 UTC](https://rubytalk.org/t/newb-class-method-determining-its-actual-class/21408/1 "2005-10-07T20:25:22Z")

</div>

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  
&nbsp;&nbsp;def A.open  
&nbsp;&nbsp;&nbsp;&nbsp;#how do we know here if the message recipient is a subclass of A?  
&nbsp;&nbsp;&nbsp;&nbsp;#i.e., can't we generalize it so that it is, in effect, class.new?  
&nbsp;&nbsp;&nbsp;&nbsp;A.new  
&nbsp;&nbsp;end  
end

Many thanks for any pointers to what I'm missing.

Art

---

<div class="post-metadata">

**Author:** ![Art\_Gillespie](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@Art\_Gillespie](https://rubytalk.org/u/Art_Gillespie)\
**Post date:** [7 October 2005 20:30 UTC](https://rubytalk.org/t/newb-class-method-determining-its-actual-class/21408/2 "2005-10-07T20:30:54Z")

</div>

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  
> > &nbsp;&nbsp;def A.open  
> > &nbsp;&nbsp;&nbsp;&nbsp;#how do we know here if the message recipient is a subclass of A?  
> > &nbsp;&nbsp;&nbsp;&nbsp;#i.e., can't we generalize it so that it is, in effect, class.new?  
> > &nbsp;&nbsp;&nbsp;&nbsp;A.new  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Many thanks for any pointers to what I'm missing.
> > 
> > Art

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [8 October 2005 11:16 UTC](https://rubytalk.org/t/newb-class-method-determining-its-actual-class/21408/3 "2005-10-08T11:16:50Z")

</div>

> 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  
> &nbsp;&nbsp;def A.open  
> &nbsp;&nbsp;&nbsp;&nbsp;#how do we know here if the message recipient is a subclass of A?  
> &nbsp;&nbsp;&nbsp;&nbsp;#i.e., can't we generalize it so that it is, in effect, class.new?  
> &nbsp;&nbsp;&nbsp;&nbsp;A.new  
> &nbsp;&nbsp;end  
> end
> 
> Many thanks for any pointers to what I'm missing.

class A  
&nbsp;&nbsp;def self.open  
&nbsp;&nbsp;&nbsp;&nbsp;new  
&nbsp;&nbsp;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:

class A  
&nbsp;&nbsp;class \<\<self  
&nbsp;&nbsp;&nbsp;&nbsp;def open  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> Art Gillespie \<agillesp@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Justin\_Bailey](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Justin\_Bailey](https://rubytalk.org/u/Justin_Bailey)\
**Post date:** [7 October 2005 21:16 UTC](https://rubytalk.org/t/newb-class-method-determining-its-actual-class/21408/4 "2005-10-07T21:16:50Z")

</div>

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  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [7 October 2005 21:18 UTC](https://rubytalk.org/t/newb-class-method-determining-its-actual-class/21408/5 "2005-10-07T21:18:11Z")

</div>

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.

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

James

> **···**
>
> --
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - The Ruby Documentation Site  
> [http://www.rubyxml.com](http://www.rubyxml.com) - News, Articles, and Listings for Ruby & XML  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [8 October 2005 11:21 UTC](https://rubytalk.org/t/newb-class-method-determining-its-actual-class/21408/6 "2005-10-08T11:21:51Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;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.
