From: Satsou Sa Date: 2010-12-31T03:02:06+09:00 Subject: Re: How to hook a callback through inheritance Josh Cheek wrote in post #971483: > The problem is that before_laughing is being invoked as a class method > on > Lol. So when you say "before_laughing :downcase_please", you aren't > setting > @before_laughing on instances of Lol, but rather on Lol itself. Okay. > I suppose one way to do this is to leave the instance var on the > class, then just ask the class for its contents and execute them > the same way. Using your code, I have the same result then Mon_Ouie, with one less method in the code and no errors when a callback isn't called. It's just so cool. > With this implementation, that means you can't add callbacks > for individual objects, though. However, I am not sure to understand this point. I believe I can do it. Here I tried to add different callbacks on different classes (and every thing is working find): >> class Lol < Lmao >> before_laughing :downcase_please >> ?> def downcase_please >> @str.downcase! >> end >> end => nil >> ?> a = Lol.new => # >> a.laughing => "hahahahahahhahahah" >> ?> class Lool < Lmao >> before_laughing :add_ok >> ?> def add_ok >> @str.concat("_ok") >> end >> end => nil >> ?> b = Lool.new => # >> b.laughing => "HAHAHAHAHAHHAHAHAH_ok" >> ?> class Loool < Lmao >> before_laughing :add_ok, :downcase_please >> ?> def add_ok >> @str.concat("_ok") >> end >> ?> def downcase_please >> @str.downcase! >> end >> end => nil >> ?> c = Loool.new => # >> c.laughing => "hahahahahahhahahah_ok" But if I understand correctly, I can not do so by class instance... Well, okay. For my needs, it's just perfect anyway. Thank you!! -- Posted via http://www.ruby-forum.com/.