From: Paul Smith Date: 2009-09-03T21:57:05+09:00 Subject: Re: Type checking function parameters 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 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 >>  if not bar.is_a? String or >>     not baz.is_a? FunkyDinosaur or >>     not quux.respond_to? "getEatenByFunkyDinosaur" >>     #complain about errors w/ raise or app specific complain function >>  end >>  # 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).  Is >> 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 and > 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 code > in an idiomatic manner whilst still covering your arse against the kind of > 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 > > > -- Paul Smith http://www.nomadicfun.co.uk paul@pollyandpaul.co.uk