Skip to content

Instantly share code, notes, and snippets.

@tastycode
Last active August 29, 2015 14:09
Show Gist options
  • Save tastycode/3da4984004f4b4a483c9 to your computer and use it in GitHub Desktop.
Save tastycode/3da4984004f4b4a483c9 to your computer and use it in GitHub Desktop.
Got a whole bunch of `fails` in your ruby codebase?
module FailClassInference
def fail(*args)
return super unless args.first.is_a?(String)
super(infer_exception_class, *args)
end
def raise(*args)
return super unless args.first.is_a?(String)
super(infer_exception_class, *args)
end
def infer_exception_class
klass = self.is_a?(Class) ? self : self.class
expected_error_klass = "StringError"
error_klass = if klass.const_defined?(expected_error_klass)
klass.const_get(expected_error_klass)
else
klass.const_set(expected_error_klass, Class.new(RuntimeError))
end
end
end
Object.include(FailClassInference)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment