William James
2/13/2006 6:39:00 AM
eastcoastcoder@gmail.com wrote:
> Very often I have a question method, which, in some cases, the caller
> would want to know why as well.
>
> Examples:
>
> def valid?
> end
>
> def abort?
> end
>
> Ruby does not allow subclassing true and false, so, if these methods
> return one of those, they can't return any additional info. But
> sometimes the caller needs additional info, as in:
>
> if !valid? logger.warn "Not valid: #{why not?}"
>
> What is the best way to handle this? I could have those methods set
> @instance_variables, but this seems a little hackish, and could
> introduce race conditions.
>
> Is there anyway to return false, "reason", or something of that sort?
> What is the preferred, idiomatic way of doing this?
def valid?( n )
return n%2==0, "It's odd."
end
f, why = valid? 9