How to undefine a method on the fly?

Suppose I have this:

class Hello
def hi
puts "Hi"
end
end
x = Hello.new

Is it possible to modify the object x so that its “hi” method no longer
works, without affecting the Hello class itself? I read about
"remove_method" and “undef_method” in ri, but I can’t figure out how to
target them at a specific object.

Suppose I have this:
class Hello
    def hi
        puts "Hi"
    end
end
x = Hello.new

   class << x
      undef hi
   end

Guy Decoux

Philip Mak wrote:

Suppose I have this:

class Hello
def hi
puts “Hi”
end
end
x = Hello.new

Is it possible to modify the object x so that its “hi” method no longer
works, without affecting the Hello class itself? I read about
“remove_method” and “undef_method” in ri, but I can’t figure out how to
target them at a specific object.

Could a workaround be to re-define/overload the method so that it
raises the appropriate exception?

···


Giuseppe “Oblomov” Bilotta

“Da grande lotterò per la pace”
“A me me la compra il mio babbo”
(Altan)
(“When I grow up, I will fight for peace”
“I’ll have my daddy buy it for me”)