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?
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?
--
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama
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?
I like this solution the best. Unlike the solutions involving exceptions or multiple return values, it still allows valid? to be used as an ordinary boolean function when you don't care about the reason why something's not valid, yet doesn't have the race conditions involved in the side-effect solutions.
That said, I think part of the reason there's no agreed upon idiom for this is because it would have been more idiomatic in Ruby to not even have the valid? method. Instead an exception would have been thrown as soon as the object became invalid, and the exception would have contained the reason. Having your class have a valid? method seems like how you'd design things in a language which doesn't have exceptions.
I like this solution the best. Unlike the solutions involving exceptions or
multiple return values, it still allows valid? to be used as an ordinary
boolean function when you don't care about the reason why something's not
valid, yet doesn't have the race conditions involved in the side-effect
solutions.
That said, I think part of the reason there's no agreed upon idiom for this
is because it would have been more idiomatic in Ruby to not even have the
valid? method. Instead an exception would have been thrown as soon as the
object became invalid, and the exception would have contained the reason.
Having your class have a valid? method seems like how you'd design things in
a language which doesn't have exceptions.
--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama