# Accessing class constants in included modules

**URL:** <https://www.ruby-forum.com/t/accessing-class-constants-in-included-modules/75203>\
**Category:** Ruby\
**Created:** [November 8, 2006, 8:17am UTC](https://www.ruby-forum.com/t/accessing-class-constants-in-included-modules/75203 "2006-11-08T08:17:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [November 8, 2006, 8:17am UTC](https://www.ruby-forum.com/t/accessing-class-constants-in-included-modules/75203/1 "2006-11-08T08:17:59Z")

</div>

module Foo  
def display  
puts self.class::A  
end  
end

class Bar  
A = 5  
include Foo  
end

class Baz  
A = 6  
include Foo  
end

Bar.new.display  
Baz.new.display

Anything cleaner-looking than self.class::Constant to access the  
constant from a method defined in the module?

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [November 9, 2006, 6:41am UTC](https://www.ruby-forum.com/t/accessing-class-constants-in-included-modules/75203/2 "2006-11-09T06:41:36Z")

</div>

Martin DeMello wrote:

> constant from a method defined in the module?
> 
> martin

Here’s a trick I use to beautify access to constants:

class Object  
def my  
self.class  
end  
end

…Now all constants can be accessed with my::Constant.

I find it more readable. Certainly less typing!

Cheers,  
Tom A.

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [November 9, 2006, 11:18am UTC](https://www.ruby-forum.com/t/accessing-class-constants-in-included-modules/75203/3 "2006-11-09T11:18:07Z")

</div>

On 11/9/06, Tom A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I find it more readable. Certainly less typing!  
> Ah - nice!

martin

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://www.ruby-forum.com/u/Martin_DeMello)\
**Post date:** [November 9, 2006, 8:58pm UTC](https://www.ruby-forum.com/t/accessing-class-constants-in-included-modules/75203/4 "2006-11-09T20:58:14Z")

</div>

Martin DeMello wrote:

> > …Now all constants can be accessed with my::Constant.
> > 
> > I find it more readable. Certainly less typing!
> 
> Ah - nice!

This may be a corner case, but if you’re going to define #my at the  
instance level, it should probably be based on the  
(singleton|eigen)class of the instance and not the “birth” class:

class Object  
def my  
self.class  
#class \<\< self; self; end  
end  
end

class Foo  
C = 2  
end

x = Foo.new  
class \<\< x  
X = 3  
end

p x.my::C  
p x.my::X

Now try it with the alternative!
