# Dynamically access class constants?

**URL:** <https://rubytalk.org/t/dynamically-access-class-constants/55816>\
**Category:** ruby-talk\
**Created:** [15 October 2009 02:33 UTC](https://rubytalk.org/t/dynamically-access-class-constants/55816 "2009-10-15T02:33:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marli\_Ba](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@Marli\_Ba](https://rubytalk.org/u/Marli_Ba)\
**Post date:** [15 October 2009 02:33 UTC](https://rubytalk.org/t/dynamically-access-class-constants/55816/1 "2009-10-15T02:33:40Z")

</div>

I've been looking for a method similar to:

Obj.send(:method)

but to access class constants. So if I have:

class MyObj  
&nbsp;&nbsp;MY\_CONSTANT\_1  
end

I want to access MyObj::MY\_CONSTANT\_1 by using a variable like foo =  
MY\_CONSTANT\_1 and then MyObj::send(:foo), but obviously send is only  
meant to work with methods and not constants.

Is there any way to do this? I've looked but am having difficulty  
finding anything.

Thanks,  
Marli

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

---

<div class="post-metadata">

**Author:** ![Justin\_Collins](https://avatars.discourse-cdn.com/v4/letter/j/3e96dc/32.png) [@Justin\_Collins](https://rubytalk.org/u/Justin_Collins)\
**Post date:** [15 October 2009 03:10 UTC](https://rubytalk.org/t/dynamically-access-class-constants/55816/2 "2009-10-15T03:10:18Z")

</div>

Marli Ba wrote:

> I've been looking for a method similar to:
> 
> Obj.send(:method)
> 
> but to access class constants. So if I have:
> 
> class MyObj  
> &nbsp;&nbsp;MY\_CONSTANT\_1  
> end
> 
> I want to access MyObj::MY\_CONSTANT\_1 by using a variable like foo =  
> MY\_CONSTANT\_1 and then MyObj::send(:foo), but obviously send is only  
> meant to work with methods and not constants.
> 
> Is there any way to do this? I've looked but am having difficulty  
> finding anything.
> 
> Thanks,  
> Marli  
> &nbsp;&nbsp;  
> I am slightly confused by your example, but you definitely need Module#const\_get: [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001689)

irb(main):001:0\> class A  
irb(main):002:1\> B = 1  
irb(main):003:1\> end  
=\> 1  
irb(main):004:0\> A.const\_get(:B)  
=\> 1

-Justin

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [15 October 2009 09:36 UTC](https://rubytalk.org/t/dynamically-access-class-constants/55816/3 "2009-10-15T09:36:03Z")

</div>

> > class MyObj  
> > &nbsp;&nbsp;MY\_CONSTANT\_1 = "hello"  
> > end

=\> "hello"

> > c = "MY\_CONSTANT\_1"

=\> "MY\_CONSTANT\_1"

> > MyObj.const\_get(c)

=\> "hello"

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

---

<div class="post-metadata">

**Author:** ![Marli\_Ba](https://avatars.discourse-cdn.com/v4/letter/m/8c91f0/32.png) [@Marli\_Ba](https://rubytalk.org/u/Marli_Ba)\
**Post date:** [15 October 2009 12:52 UTC](https://rubytalk.org/t/dynamically-access-class-constants/55816/4 "2009-10-15T12:52:16Z")

</div>

Justin Collins wrote:

> I am slightly confused by your example, but you definitely need  
> Module#const\_get:  
> [class Module - RDoc Documentation](http://www.ruby-doc.org/core/classes/Module.html#M001689)
> 
> irb(main):001:0\> class A  
> irb(main):002:1\> B = 1  
> irb(main):003:1\> end  
> =\> 1  
> irb(main):004:0\> A.const\_get(:B)  
> =\> 1
> 
> -Justin

Ah, sorry for the confusion. const\_get is exactly what I was looking  
for though, thanks!

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