The first thing to keep in mind is that monkey-patching a class directly — that is, opening a class to define a new method — is discouraged. It works, but it's not very flexible and it's considered a code smell.
A more sensible approach to monkey-patching is to define your methods in a mixin and then including it in a class.
This also allows you to conditionally include the mixin. For example, a common requirement in Ruby Gems is to only implement or define something if another library is (already) loaded. A common way to do this is to check if a constant from that library is defined. For example, in your case you could do this:
module PresenceExtensions def blank? respond_to?(:empty?) ? (respond_to?(:strip) ? strip.empty? : !!empty?) : !self end end unless Module.const_defined?("Rails") || Object.method_defined?(:blank?) Object.include PresenceExtensions end
Another common technique is to try to load a gem and then add your alternative monkey-patch only if the gem is not available:
begin require "active_support/core_ext/object/blank" rescue LoadError Object.include PresenceExtensions end
This technique has the advantage that will tell you immediately if the gem is not available, so that you don't have to worry about load order.