Ara.T.Howard said:
3)
tuples = db.execute sql
tuple = tuples.first
answer = tuple.first
raise "something terrible has happened in the database" unless
answer == 42
Actually, (3) is even worse that it looks here, because you have to test
tuple before sending #first to it, if you want the same semantics as (1)
and (2).
(3b)
tuples = db.execute sql
tuple = tuples.first
raise "something terrible has happened in the database" unless tuple
answer = tuple.first
raise "something terrible has happened in the database" unless
answer == 42
I guess you could put a "rescue NoMethodError" clause around everything,
but that would hide any NoMethodError that came up in, say, #first. So
the argument for (1) is pretty strong...
OTOH, (3b) has the advantage that you can make the error message more
informative in each of the two error cases.
If someone wants a conditional with a short-circuitable sequence of
tests, but without the compact elegance of (1), they could do something
like this:
def the_following_checks_succeed
catch :fail do yield end
end
def check
yield or throw :fail, false
end
tuples = [ [42,2,3], [4,5,6] ]
raise "something's wrong" unless the_following_checks_succeed do
check {tuples}
tuple = check {tuples[0]}
answer = check {tuple[0]}
answer == 42 # or: check {answer == 42}
end
puts "Now, what is the question?"