Get constant value by using string?

Hi,

My question is really simple :slight_smile:

class MyClass
  ANYTHING = "huh"
end

How can I print out huh from string?

MyClass::"anything".upcase

Is there any way to do that ?

:slight_smile:

Thanks for help...

···

--
Posted via http://www.ruby-forum.com/.

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

Sure, for example:

   MyClass.module_eval("ANYTHING")
   MyClass.const_get("ANYTHING")

In Rails you also have:

   "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 ?

:slight_smile:

Thanks :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.