# How to figure out what class a method is in for a \*class met

**URL:** <https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108>\
**Category:** Ruby\
**Created:** [April 21, 2006, 1:15am UTC](https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108 "2006-04-21T01:15:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [April 21, 2006, 1:15am UTC](https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108/1 "2006-04-21T01:15:37Z")

</div>

Hi all -

Given the following bit of code, I’d like it to spit out “Cat” followed  
by  
“Dog”, but what I get now is “Object” twice.

* * *

class Animal  
def self.what\_am\_i  
self.class  
end  
end

class Cat \< Animal  
end

class Dog \< Animal  
end

## puts Cat.what\_am\_i puts Dog.what\_am\_i

The closest I can get is self.object\_id which prints out different ID’s,  
but I’d really like the name if I can get it…

Thanks!

-philip

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [April 21, 2006, 1:18am UTC](https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108/2 "2006-04-21T01:18:38Z")

</div>

> end
> 
> The closest I can get is self.object\_id which prints out different ID’s, but  
> I’d really like the name if I can get it…

Naturally I find it right after posting…

self.name does the trick.

-philip

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [April 21, 2006, 1:27am UTC](https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108/3 "2006-04-21T01:27:42Z")

</div>

On 4/20/06, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> end

Try  
def self.what\_am\_i  
self.name  
end

Because self is an instance of Class, you want to ask it what its name  
is.

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [April 21, 2006, 10:31am UTC](https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108/4 "2006-04-21T10:31:01Z")

</div>

I have a related question - a similar problem to the one just given,  
but that solution doesn’t fit here because I don’t just want the class  
name - I want an attribute associated with the class, which subclasses  
can override.

I’ve tried doing this using class variables, but it doesn’t work:

–

class Animal  
@@sound = “nothing”  
def to\_s  
"I say " + @@sound  
end  
end

class Cat \< Animal  
@@sound = “miaow”  
end

class Dog \< Animal  
@@sound = “woof”  
end

fudge = Cat.new()  
puts fudge

tip = Dog.new()  
puts tip

–

This is an abstraction of my real situation but it captures the  
essense: each subclass has some attribute which needs to be  
manipulated at some point; the code to manipulate the attribute is  
common across all subclasses, so it should live in the superclass.  
(In fact, ideally, Animal is “abstract” and I don’t want to declare a  
@@sound there at all - but if I do that, to\_s complains about  
“uninitialized class variable”.)

Alas:

[gimbo@lotus learn] ruby -v subclass\_names.rb  
ruby 1.8.4 (2005-12-24) [i386-freebsd6]  
subclass\_names.rb:9: warning: already initialized class variable @@sound  
subclass\_names.rb:13: warning: already initialized class variable  
@@sound  
I say woof  
I say woof

So I guess I’m misusing class variables.

I also tried it with constants (which would be even better since the  
value won’t ever change) but got “I say nothing” twice instead.

If anyone can help me understand what’s going on and how this _should_  
be done, I’d be grateful. I’m still thinking in python a bit too  
much, I think.

Thanks,

-Andy

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [April 21, 2006, 11:32am UTC](https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108/5 "2006-04-21T11:32:58Z")

</div>

On 4/21/06, Andy G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Animal  
> class Dog \< Animal  
> @@sound = “woof”  
> end
> 
> fudge = Cat.new()  
> puts fudge
> 
> tip = Dog.new()  
> puts tip
> 
> –

class Animal  
@sound = “nothing”  
class \<\< self  
attr\_accessor :sound  
end  
def to\_s  
"I say " + self.class.sound  
end  
end

class Cat \< Animal  
self.sound = “miaow”  
end

class Dog \< Animal  
self.sound = “woof”  
end

fudge = Cat.new()  
puts fudge

tip = Dog.new()  
puts tip

That’s how ruby treats it, there are some who argue that @@ should go  
away,  
maybe they are right.

Hope that helps  
Robert

> –  
> Andy G.  
> Computer Science Department  
> University of Wales Swansea  
> [http://www.cs.swan.ac.uk/~csandy/](http://www.cs.swan.ac.uk/~csandy/)

–  
Deux choses sont infinies : l’univers et la bÃªtise humaine ; en ce qui  
concerne l’univers, je n’en ai pas acquis la certitude absolue.

- Albert Einstein

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [April 21, 2006, 11:36am UTC](https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108/6 "2006-04-21T11:36:05Z")

</div>

2006/4/21, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> end
> 
> The closest I can get is self.object\_id which prints out different ID’s,  
> but I’d really like the name if I can get it…

Let me get this straight: you have a class object and you want a  
method that returns this class object? The code that does what you  
want is this:

def self.what\_am\_i?  
self  
end

But this seems a rather weird thing to do…

Did you actually try this?

puts Cat, Dog

You can even use the name, but the output won’t differ

puts Cat.name, Dog.name

If your problem is something else, please let us know.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![phallstrom](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f475e1/32.png) [@phallstrom](https://www.ruby-forum.com/u/phallstrom)\
**Post date:** [April 21, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/how-to-figure-out-what-class-a-method-is-in-for-a-class-met/94108/7 "2006-04-21T18:46:12Z")

</div>

> > ```
> > end
> > 
> > ```
> > 
> > * * *
> 
> end
> 
> But this seems a rather weird thing to do…

🙂 This is just the simplest example I could think of. The actual  
method in the parent class is responsible for storing “stuff” in  
memcache  
and I want to dynamically set the key used to store it to the class name  
and the primary key. That way I won’t have to worry about collisions.

For the archives, seems that

def self.what\_am\_i  
put self  
put self.name  
end

will do exactly what I want (return the name of the calling class)

Thanks all!

-philip
