From: Robert Klemme Date: 2007-04-18T17:00:05+09:00 Subject: Re: nil in evaluations Please don't top post. On 18.04.2007 05:08, progcat@comcast.net wrote: > Close but not really what I was looking for. > > example: > if g(x) > f(x) > then > put "rejoice because f(x) is greater than f(x)" > else > put "g(x) <= f(x) OR g(x) and/or f(x) returned a nil so I > wanted to execute this" > end. > > In other words I want the function to work as it normally would for > valid values, and if one or both are nil I want to make the test > evaluate to false. Personally I would not mess with operators. There would be too many places to tackle. Also this can have undesired side effects. The main question is under which circumstances to those functions return nil? If it is for invalid input I'd rather raise an exception (ArgumentError) or leave the code as is (i.e. let the comparison throw) although I'd prefer to fail fast (i.e. in those functions). If you do not want to change your code, you can do this: >> if (4 > nil rescue false) >> p 1 >> else ?> p 2 >> end 2 => nil I.e. use a "rescue" in the expression. The brackets are mandatory to avoid parsing errors. Kind regards robert