Just define a method for the object:
class Thing def greeting 'yo, man' end end Thing.instance_methods(false) #=> [:greeting] object = Thing.new #=> #<Thing:0x007fc4ba276c90> object.greeting #=> "yo, man"
Define two methods in object (which will be instances of the method in the object singleton class.
def object.greeting 'hey, dude' end def object.farewell 'see ya' end object.methods(false) #=> [:greeting, :farewell] object.singleton_class.instance_methods(false) #=> [:greeting, :farewell] object.greeting #=> "hey, dude" object.farewell #=> "see ya" new_obj = Thing.new new_obj.greeting #=> "yo, man" new_obj.farewell #NoMethodError: undefined method `farewell' for #<Thing:0x007fe5a406f590>
Remove the object singleton greeting method.
object.singleton_class.send(:remove_method, :greeting) object.methods(false) #=> [:farewell] object.greeting #=> "yo, man"
Another way to remove :greeting from the object singleton class is as follows.
class << object remove_method(:greeting) end
source share