# Get constant value by using string?

**URL:** <https://rubytalk.org/t/get-constant-value-by-using-string/42403>\
**Category:** ruby-talk\
**Created:** [21 November 2007 20:28 UTC](https://rubytalk.org/t/get-constant-value-by-using-string/42403 "2007-11-21T20:28:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jamal\_Soueidan](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jamal\_Soueidan](https://rubytalk.org/u/Jamal_Soueidan)\
**Post date:** [21 November 2007 20:28 UTC](https://rubytalk.org/t/get-constant-value-by-using-string/42403/1 "2007-11-21T20:28:44Z")

</div>

Hi,

My question is really simple 🙂

class MyClass  
&nbsp;&nbsp;ANYTHING = "huh"  
end

How can I print out huh from string?

MyClass::"anything".upcase

Is there any way to do that ?

🙂

Thanks for help...

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

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [21 November 2007 20:33 UTC](https://rubytalk.org/t/get-constant-value-by-using-string/42403/2 "2007-11-21T20:33:20Z")

</div>

Jamal Soueidan wrote:

> MyClass::"anything".upcase
> 
> Is there any way to do that ?

MyClass.const\_get("anything".upcase)

HTH,  
Sebastian

> **···**
>
> --  
> NP: Porcupine Tree - Shallow  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)\
**Post date:** [21 November 2007 20:33 UTC](https://rubytalk.org/t/get-constant-value-by-using-string/42403/3 "2007-11-21T20:33:56Z")

</div>

Sure, for example:

&nbsp;&nbsp;&nbsp;MyClass.module\_eval("ANYTHING")  
&nbsp;&nbsp;&nbsp;MyClass.const\_get("ANYTHING")

In Rails you also have:

&nbsp;&nbsp;&nbsp;"MyClass::ANYTHING".constantize

-- fxn

> **···**
>
> On Nov 21, 2007, at 9:28 PM, Jamal Soueidan wrote:
> 
> > class MyClass  
> > ANYTHING = "huh"  
> > end
> > 
> > How can I print out huh from string?
> > 
> > MyClass::"anything".upcase
> > 
> > Is there any way to do that ?
> > 
> > 🙂

---

<div class="post-metadata">

**Author:** ![Jamal\_Soueidan](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jamal\_Soueidan](https://rubytalk.org/u/Jamal_Soueidan)\
**Post date:** [21 November 2007 21:15 UTC](https://rubytalk.org/t/get-constant-value-by-using-string/42403/4 "2007-11-21T21:15:55Z")

</div>

Thanks 🙂

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