My first stab at some Ruby started like this too.

Let it go.

Stop caring about what type your parameters are, and just let them
complain if you try to call a method which they don't respond to.

It's unsatisfying at first, if you've been brainwashed into writing
code where you check everything to try to prevent illegal instructions
from happening, but you'll soon realise that it actually gives you a
lot of freedom and power.

If it walks like a duck and quacks like a duck then it's probably a duck.

On Thu, Sep 3, 2009 at 9:49 AM, Eleanor
McHugh<eleanor / games-with-brains.com> wrote:
> On 3 Sep 2009, at 05:04, Nick Green wrote:
>>
>> More or less all my functions look something like
>>
>> def foo bar baz quux
>> =A0if not bar.is_a? String or
>> =A0 =A0 not baz.is_a? FunkyDinosaur or
>> =A0 =A0 not quux.respond_to? "getEatenByFunkyDinosaur"
>> =A0 =A0 #complain about errors w/ raise or app specific complain functio=
n
>> =A0end
>> =A0# do some stuff
>> end
>>
>>
>> There has got to be a better way to go about this parameter checking
>> business, but googling is not working (bad search terms maybe). =A0Is
>> there?
>>
>> And I don't really mean other ways of writing the same logic, i.e.
>>
>> 3 statements that look like:
>>
>> complain "baz is no dinosaur!" unless baz.is_a? FunkyDinosaur
>>
>> does not seem a whole lot better to me than the above example
>
> You're looking at the problem inside-out which is why you're finding
> yourself in such a tangle: in Ruby explicit type checking is very rare an=
d
> in cases where you think you need it you're generally wrong and fighting
> against the language rather than flowing with it. Google the term "Duck
> Typing" and you should find lots of helpful advice on how to write your c=
ode
> in an idiomatic manner whilst still covering your arse against the kind o=
f
> type errors which can occur using Exception handling.
>
>
> Ellie
>
> Eleanor McHugh
> Games With Brains
> http://slides.games-with-brains.net
> ----
> raise ArgumentError unless @reality.responds_to? :reason
>
>
>



--=20
Paul Smith
http://www.nomadicfun.co.uk

paul / pollyandpaul.co.uk