How to add an after_save callback on runtime on a particular instance

class User < AR
end

user = User.new

Let’s say that I have a user instance with me. After this record is
saved I want an after_save callback which would print the id of the
record.
The only catch is that I am not allowed to change the User class. How
can I accomplish my goal? Is that even possible to add an after_save
callback on a particular instance object. Any metaprogramming hack.
I’m sure ruby does provide some tool.

This would be a trivial issue if I were allowed to change the User
class but I am not.

On 12 May 2010 03:15, Nadal [email protected] wrote:

can I accomplish my goal? Is that even possible to add an after_save
callback on a particular instance object. Any metaprogramming hack.
I’m sure ruby does provide some tool.

This would be a trivial issue if I were allowed to change the User
class but I am not.

What do you mean by ‘not allowed to change the User class’? Do you
mean that you are not allowed to change the file user.rb? If so that
is not a problem as you can modify or extend the class in another
loaded file merely by providing class User < … and providing new or
modified methods there.

Colin

On May 12, 3:15 am, Nadal [email protected] wrote:

callback on a particular instance object. Any metaprogramming hack.
I’m sure ruby does provide some tool.

well on top of what Colin says you can do

def user.some_method

end

or user.extend SomeModule

I think both will prevent you from marshalling the object and I seem
to remember that in MRI both will cause ruby to dump it’s cache of
method names to implementations

Fred