# How to find self class name inside self difinition?

**URL:** <https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110>\
**Category:** ruby-talk\
**Created:** [10 November 2008 05:32 UTC](https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110 "2008-11-10T05:32:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thani\_Ararsu](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@Thani\_Ararsu](https://rubytalk.org/u/Thani_Ararsu)\
**Post date:** [10 November 2008 05:32 UTC](https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110/1 "2008-11-10T05:32:48Z")

</div>

module Aa  
end

class Aa::Name  
&nbsp;&nbsp;def self.class\_name  
&nbsp;&nbsp;&nbsp;&nbsp;[self.class.name](http://self.class.name)  
&nbsp;&nbsp;end  
end  
puts Aa::Name.class\_name # 'Class'

# But i want my class name as 'Aa::Name' instead of 'Class'

any idea ?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Shot\_Piotr\_Szotkowsk](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@Shot\_Piotr\_Szotkowsk](https://rubytalk.org/u/Shot_Piotr_Szotkowsk)\
**Post date:** [10 November 2008 06:28 UTC](https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110/2 "2008-11-10T06:28:15Z")

</div>

Thani Ararsu:

> module Aa  
> end

> class Aa::Name  
> &nbsp;&nbsp;def self.class\_name  
> &nbsp;&nbsp;&nbsp;&nbsp;self.class.name  
> &nbsp;&nbsp;end  
> end  
> puts Aa::Name.class\_name # 'Class'

(This is correct; the object ‘Aa::Name’ is of class ‘Class’.)

> # But i want my class name as 'Aa::Name' instead of 'Class'

You want the Module#name method:

class Aa::Name  
&nbsp;&nbsp;def self.class\_name  
&nbsp;&nbsp;&nbsp;&nbsp;name  
&nbsp;&nbsp;end  
end

Aa::Name.class\_name # =\> 'Aa::Name'

-- Shot

> **···**
>
> --  
> MovableType is why my local whisky merchant isn't going out  
> of business any time soon. -- Peter Corlett, hates-software

---

<div class="post-metadata">

**Author:** ![Stefan\_Rusterholz](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stefan\_Rusterholz](https://rubytalk.org/u/Stefan_Rusterholz)\
**Post date:** [10 November 2008 06:30 UTC](https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110/3 "2008-11-10T06:30:17Z")

</div>

Thani Ararsu wrote:

> module Aa  
> end
> 
> class Aa::Name  
> &nbsp;&nbsp;def self.class\_name  
> &nbsp;&nbsp;&nbsp;&nbsp;self.class.name  
> &nbsp;&nbsp;end  
> end  
> puts Aa::Name.class\_name # 'Class'
> 
> # But i want my class name as 'Aa::Name' instead of 'Class'
> 
> any idea ?

Just drop the .class part.  
class Aa::Name  
&nbsp;&nbsp;def self.class\_name; self.name; end # self is redundant so:  
&nbsp;&nbsp;def self.class\_name; name; end # now for this we got a nice little  
construct that goes:  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;alias class\_name name  
&nbsp;&nbsp;end  
end

Regards  
Stefan

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Xue19840909](https://avatars.discourse-cdn.com/v4/letter/x/e9bcb4/32.png) [@Xue19840909](https://rubytalk.org/u/Xue19840909)\
**Post date:** [10 November 2008 10:08 UTC](https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110/4 "2008-11-10T10:08:06Z")

</div>

module Aa  
end

class Aa::Name  
&nbsp;&nbsp;def class\_name  
&nbsp;&nbsp;&nbsp;&nbsp;[self.class.name](http://self.class.name)  
&nbsp;&nbsp;end  
end  
puts Aa::Name.new().class\_name

---

<div class="post-metadata">

**Author:** ![Stephen\_Pearson](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Stephen\_Pearson](https://rubytalk.org/u/Stephen_Pearson)\
**Post date:** [10 November 2008 11:17 UTC](https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110/5 "2008-11-10T11:17:43Z")

</div>

Thani Ararsu wrote:

> module Aa  
> end
> 
> class Aa::Name  
> &nbsp;&nbsp;def self.class\_name  
> &nbsp;&nbsp;&nbsp;&nbsp;self.class.name  
> &nbsp;&nbsp;end  
> end  
> puts Aa::Name.class\_name # 'Class'
> 
> # But i want my class name as 'Aa::Name' instead of 'Class'
> 
> any idea ?

module Aa  
end

class Aa::Name  
&nbsp;&nbsp;&nbsp;def self.class\_name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name  
&nbsp;&nbsp;&nbsp;end  
end  
puts Aa::Name.class\_name

Or dispense with the class method entirely:

puts Aa::Name.name

---

<div class="post-metadata">

**Author:** ![Thani\_Ararsu](https://avatars.discourse-cdn.com/v4/letter/t/df705f/32.png) [@Thani\_Ararsu](https://rubytalk.org/u/Thani_Ararsu)\
**Post date:** [10 November 2008 09:25 UTC](https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110/6 "2008-11-10T09:25:36Z")

</div>

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [10 November 2008 13:47 UTC](https://rubytalk.org/t/how-to-find-self-class-name-inside-self-difinition/50110/7 "2008-11-10T13:47:16Z")

</div>

> module Aa  
> end
> 
> class Aa::Name  
> def self.class\_name  
> &nbsp;&nbsp;&nbsp;name  
> end  
> end  
> puts Aa::Name.class\_name
> 
> Or dispense with the class method entirely:

That would be my advice as well. Note also that Module#to\_s is an  
alias for Module#name so you can even print the class object directly  
(if it is for output purposes).

> puts Aa::Name.name

Ruby version 1.8.7  
irb(main):001:0\> module Aa  
irb(main):002:1\> class Name  
irb(main):003:2\> puts name, self.name, self  
irb(main):004:2\> end  
irb(main):005:1\> end  
Aa::Name  
Aa::Name  
Aa::Name  
=\> nil  
irb(main):006:0\>

Kind regards

robert

> **···**
>
> 2008/11/10 Stephen Pearson \<steve.6qa8e@mailnull.com\>:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end
