From: "David A. Black" Date: 2009-09-07T19:41:37+09:00 Subject: Re: Type checking function parameters Hi -- On Mon, 7 Sep 2009, Nick Green wrote: > Lots of helpful opinions/philosophies about ruby here. > > As for the respond_to? is more useful than is_a? comment, I typecheck > almost everything with respond to (ok thats not really a "type"check... > i... check things with respond_to?) Actually respond_to? is the closest thing to a built-in tool that Ruby offers for type-checking. is_a? just checks class and class/module ancestry. > However, heres one issue I have, its not complicated, but its simpler to > say in code than words: > > def foo someint > someint+7 > end > > if foo "hello" > puts "hi" > else > puts "oh noez!" > end > > Putting this in irb only gives me an error. There are times I would > like to say, if this function works, do this, otherwise, do this. And > its not neccesarily unacceptable for the function to fail, but I wan't > to know if it failed so I can do something about that. I realize that > many (most?) functions you just figure its bad news if it fails, but I > have enough functions that I just redo/try something else/figure out > whats wrong if the function fails that I need a way to say this. You'll get an exception there which you can intercept and handle. If you're really cascading through various methods until you find one that doesn't blow up, that's about the only way to do it. It sounds a bit fragile, though. David -- David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com Ruby/Rails training, mentoring, consulting, code-review Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2) September Ruby training in NJ has been POSTPONED. Details to follow.