Create a Constant out of a String's value

Hi all

I know that classes are some sort of Constants in ruby. Now I have to
dynamically call a class’ method while the class’ name is in a string.

string = “MyCoolClass”

Now I wanna call MyCoolClass.my_cool_method but I only have the string
“string”. You know what I mean?

Thanks :slight_smile:
Josh

Joshua M. wrote:

string = “MyCoolClass”

Now I wanna call MyCoolClass.my_cool_method but I only have the string
“string”. You know what I mean?

string.constantize.my_cool_method


Jakob S. - http://mentalized.net

On 9/25/06, Jakob S. [email protected] wrote:

Jakob S. - http://mentalized.net

Or in plain Ruby:

Object.const_get(“MyCoolClass”).my_cool_method

Max

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs