get constant value by using string?

J

Jamal Soueidan

Hi,

My question is really simple :)

class MyClass
ANYTHING = "huh"
end

How can I print out huh from string?

MyClass::"anything".upcase

Is there any way to do that ?

:)

Thanks for help...
 
X

Xavier Noria

class MyClass
ANYTHING = "huh"
end

How can I print out huh from string?

MyClass::"anything".upcase

Is there any way to do that ?

:)

Sure, for example:

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

In Rails you also have:

"MyClass::ANYTHING".constantize

-- fxn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,270
Messages
2,571,353
Members
48,038
Latest member
HunterDela

Latest Threads

Top