You can explicitly prevent an object from being extended it by "freezing" it using the freeze method:
ob.freeze
Any attempt to extend this object further would result in a runtime error:
module D def method_d puts( 'hello from d' ) end end ob.extend( D ) #=> Error: can't modify frozen object (RuntimeError)
You can use the frozen? method to test whether an object has been frozen:
if !(ob.frozen?) ob.extend( D ) ob.method_d else puts( "Can't extend a frozen object" ) end