Ruby mixins and calling super methods?

You can use this: super if defined?(super) Here is an example: class A end class B.

This worked great for me – Nathan Oct 10 '10 at 6:21.

Rather than checking if the super method exists, you can just define it class ActiveRecord::Base def after_initialize end end This works in my testing, and shouldn't break any of your existing code, because all your other classes which define it will just be silently overriding this method anyway.

The including class (the thing that inherits from ActiveRecord::Base, which, in this case is Iso) could define its own after_initialize, so any solution other than alias_method_chain (or other aliasing that saves the original) risks overwriting code. @Orion Edwards' solution is the best I can come up with. There are others, but they are far more hackish.

Alias_method_chain also has the benefit of creating named versions of the after_initialize method, meaning you can customize the call order in those rare cases that it matters. Otherwise, you're at the mercy of whatever order the including class includes the mixins. Later: I've posted a question to the ruby-on-rails-core mailing list about creating default empty implementations of all callbacks.

The saving process checks for them all anyway, so I don't see why they shouldn't be there. The only downside is creating extra empty stack frames, but that's pretty cheap on every known implementation.

You can just throw a quick conditional in there: super if respond_to?('super') and you should be fine - no adding useless methods; nice and clean.

This seems to work. – James Baker May 11 '10 at 22:31 This does not appear to work. Respond_to?('super') will always return false.

– averell Jul 15 '10 at 11:53.

I actually tested alias_method_chain before posting my previous comment. It fails for the same reason - at some point there needs to be an after_initialize method in ActiveRecord::Base, or test for one existing.

You can basically chained up all your after_initialize calls. It acts like a decorator: each new method adds a new layer of functionality and passes the control onto the "overridden" method to do the rest.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions