how is it possible to add a method to a single object programatically?
def obj.meth
…
end
(and a few other ways, but that’s the simplest)
The method shall rename a method for a specific instance of any class, e.g.
→ Expected behaviour
I already succeeded in renaming methods for whole classes, but for a single
instance?
Keep in mind that a single instance has a class of its own – its
singleton class – as well as its class of origin. If you do the
renaming in the singleton class, it will affect only that object.
Have a look at this example, and see if it helps:
class C
def m
puts “In the method”
end
end
c = C.new
c.m # In the method
class << c # “<< c” invokes the singleton class of c
alias n m
undef_method(“m”)
end
c.n # In the method
c.m # error – no such method
Hmmm… one problem is that Fixnums don’t have singleton classes, so
this wouldn’t work for 5. Anyway, maybe it will give you ideas