rescue MethodError
can be replaced with respond_to?
RB-P1001Rescuing NoMethodError
can be upto 8x slower than checking for the method using respond_to?
.
rescue
expects the exception to be raised first, hence it is much slower.
def slow
begin
writing
rescue NoMethodError
# Do something else...
end
end
def fast
if respond_to?(:writing)
writing
else
# Do something else...
end
end