[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

get constant value by using string?

Jamal Soueidan

11/21/2007 8:29:00 PM

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...
--
Posted via http://www.ruby-....

3 Answers

Sebastian Hungerecker

11/21/2007 8:33:00 PM

0

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

Xavier Noria

11/21/2007 8:34:00 PM

0

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 ?
>
> :)

Sure, for example:

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

In Rails you also have:

"MyClass::ANYTHING".constantize

-- fxn


Jamal Soueidan

11/21/2007 9:16:00 PM

0

Thanks :)
--
Posted via http://www.ruby-....