Ever been sick of blocks not handling nil in Ruby, or more specifically Rails?
This gem defines the NilPasser
class, which can be used to monitor all passing of blocks on an object, through a specific method, or to a class.
Each block passed is passed nil
and the execution usually continues normally. (Usually meaning you probably don't want to use this in production.)
During or after execution, you can grep '^[no_nil]'
on your logs to get all the results.
Loosely inspired by the bullet
gem.
From the tests, we have example usage for "good, bad, and subtle" blocks:
This is the control: we provide the caller's location and the identity (function) block (source):
def test_test_ignores_good_block
assert @log.blank?
NilPasser.test [Rails.path], Proc.new{|x| x}
assert @log.blank?
end
This is a simple exception-handling case, where the block accepts nil
but always raises an exception (source):
def test_test_catches_bad_block
assert @log.blank?
NilPasser.test [Rails.path], Proc.new{|x| (raise "hi")}
assert [email protected]?
end
This is an example of a block that only raises an exception when passed nil
(source):
def test_test_catches_subtle_block
assert @log.blank?
NilPasser.test [Rails.path], Proc.new{|x| x.nil? && (raise "hi")}
assert [email protected]?
end