String of Parameters used as parameter list

Hi there,

I know this might sound unorthodox but I am storing a function and
parameters in a database for a particular set of class methods. Most of
the functions have the same arguments but a few others have extra
parameters after.

So instead of having a separate column for each parameter I am instead
putting all the parameters in a single database column which I want to
pass so I would like to do something like this:

class Klass
def self.some_function(age, weight, name); end
end

function_name = “some_function”
function_args = “12, 14, ‘james’”
Klass.send function_name, function_args

Is there any way to do this? I have looked at things like *args but I
would really like to keep the signature of the function the same because
it’d require a lot of refactoring otherwise.

Cheers,
James

On Fri, Oct 9, 2009 at 8:39 AM, James S. [email protected]
wrote:

function_name = “some_function”
function_args = “12, 14, ‘james’”
Klass.send function_name, function_args

Is there any way to do this? I have looked at things like *args but I
would really like to keep the signature of the function the same because
it’d require a lot of refactoring otherwise.

1 then you may need to adjust the calling, eg

Klass.send function_name, *function_args.split(",")

2 or you can use eval or module_eval also

kind regards -botp

Great, thanks botp. I figured out the * syntax about 10 minutes ago =)
You were spot on with that though, thank you.

botp schrieb:

1 then you may need to adjust the calling, eg

Klass.send function_name, *function_args.split(",")

The function_args were two Fixnums and one String, after this you have
“12” and " 14" instead of the numbers.

So you’d better put the args in an array (before they become Strings)
and ‘Marshal.dump array’ this and later

Klass.send function_name, *Marshal.load(function_args)

We should use eval when there is another way.

R.

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