String of Parameters used as parameter list

J

James Somers

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
 
B

botp

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
2 or you can use eval or module_eval also

kind regards -botp
 
J

James Somers

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

Rüdiger Bahns

botp said:
1 then you may need to adjust the calling, eg

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.
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top