[C.L.R],[RoR] How I convert string to method?


#1

Hi,

suppose I have 2 strings:

s1 = “hello”
s2 = “capitalize”

I want to do this:

m = s2.to_method
s1.m
=> “Hello”

I’m working on a Rails project where I want to call arbitrary methods of
a
model class.

At run-time …
I know how to get the names of the methods,
but can’t figure out how to call the corresponding methods.

How I convert string to method?

-Peter


#2

I found the answer in the Pick Axe book:

s1.send(s2.to_sym)

HTH
-Peter


#3

Peter S. wrote:

I’m working on a Rails project where I want to call arbitrary methods of a
model class.

“hello”.send(“capitalize”.to_sym)
=> “Hello”

Or probably even better send instead of send.

Zsombor

Company - http://primalgrasp.com
Thoughts - http://deezsombor.blogspot.com