I want to use an assert to raise an error within a rake task.
the_index = items.index(some_item)
assert_not_nil the_index, "Lookup failed for the following item: " + some_item
I get undefined method assert_not_nil. Can I include the assertions file in my rake task? How?
Is this a best practice, or is there a better way to do it?
Working in Ruby 1.9.2.
There is a built-in
Array#fetchmethod that acts like#[]but raises an IndexError instead of returning nil when the element is not found. (The same applies for Hash.) I always use the first one if I don’t expect the collection to exclude an element.And for the other cases raise exceptions yourself like Bramha Ghosh suggests:
However, you shouldn’t be doing this often, only if you know that your code will fail far away making debugging painful.