K
Ken Coar
I have a number of methods that can take arguments in two different
forms. As an example:
method(tuple [, ...])
where 'tuple' can be things like (Point, Point, Integer) or (Numeric,
Numeric, Numeric, Numeric, Integer) (so either a Point object
representing the coordinates, or the coordinates directly).
I realise that type-checking in Ruby is anathematic to some, but I want
to generate some intelligent error messages in some cases. So please
let's leave the 'why do you want to do that' bit alone for now.
One meta-thought I had was for the argument vetter to be passed the
arglist and an array of allowed patterns, like
[
%r/^(??oint\s*){2}\s*Integer)+$/,
%r/^(??:Numeric\s*){4}\s*Integer)+$/
]
and check each tuple by doing kind_of?() of each element against the
appropriate words in the pattern.
Which is actually turning out to be a right bugger to code, actually.
So I'm figuring maybe someone could suggest a better method to me?
Thanks!
forms. As an example:
method(tuple [, ...])
where 'tuple' can be things like (Point, Point, Integer) or (Numeric,
Numeric, Numeric, Numeric, Integer) (so either a Point object
representing the coordinates, or the coordinates directly).
I realise that type-checking in Ruby is anathematic to some, but I want
to generate some intelligent error messages in some cases. So please
let's leave the 'why do you want to do that' bit alone for now.
One meta-thought I had was for the argument vetter to be passed the
arglist and an array of allowed patterns, like
[
%r/^(??oint\s*){2}\s*Integer)+$/,
%r/^(??:Numeric\s*){4}\s*Integer)+$/
]
and check each tuple by doing kind_of?() of each element against the
appropriate words in the pattern.
Which is actually turning out to be a right bugger to code, actually.
So I'm figuring maybe someone could suggest a better method to me?
Thanks!