R
Ronald Fischer
Can someone point out an elegant way to solve the following problem?
I have a module with a function f and additional functions g1, g2, ...,=20
which basically looks like this:
module M
def f(&cb)
...
x=3Dcb.call
# DO SOMETHING .... see below
end
def g1(a,b,c)
...
end
def g2(d,e)
...
end
# etc
end
The DO SOMETHING part looks like this: The proc object being called=20
is supposed to return an array, where the first element is a symbol
corresponding to one of the function names g1, g2 ...., and the=20
remaining array elements corresponding to the parameters of this
function. Hence, if=20
x=3Dcb.call
causes x to contain [:g2, 25, 'dummy'], I would like to execute
g2(25,'dummy')
I have not found a convincing way of doing this. Of course I can
use a big case ... end on the function names (ugly). I can turn
the symbol :g2 into a string 'g2', marshal the arguments and put
together a string containing the function call (even uglier).
I also thought about using a hash of proc object (the keys being
the symbols, the values being the procedures), kind of
{:g1 =3D> proc {|a,b,c] g1(a,b,c)}, :g2 =3D> proc {|d,e| g2(d,e)
},...}[x[0]].
call(*x[1..-1])
but this looks like tremendous overkill too. Is there a simpler
way of doing it?
Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
I have a module with a function f and additional functions g1, g2, ...,=20
which basically looks like this:
module M
def f(&cb)
...
x=3Dcb.call
# DO SOMETHING .... see below
end
def g1(a,b,c)
...
end
def g2(d,e)
...
end
# etc
end
The DO SOMETHING part looks like this: The proc object being called=20
is supposed to return an array, where the first element is a symbol
corresponding to one of the function names g1, g2 ...., and the=20
remaining array elements corresponding to the parameters of this
function. Hence, if=20
x=3Dcb.call
causes x to contain [:g2, 25, 'dummy'], I would like to execute
g2(25,'dummy')
I have not found a convincing way of doing this. Of course I can
use a big case ... end on the function names (ugly). I can turn
the symbol :g2 into a string 'g2', marshal the arguments and put
together a string containing the function call (even uglier).
I also thought about using a hash of proc object (the keys being
the symbols, the values being the procedures), kind of
{:g1 =3D> proc {|a,b,c] g1(a,b,c)}, :g2 =3D> proc {|d,e| g2(d,e)
},...}[x[0]].
call(*x[1..-1])
but this looks like tremendous overkill too. Is there a simpler
way of doing it?
Ronald
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162