# How to alias\_method\_chain initialize from a mixin

**URL:** <https://www.ruby-forum.com/t/how-to-alias-method-chain-initialize-from-a-mixin/179327>\
**Category:** Ruby\
**Created:** [November 22, 2009, 8:09am UTC](https://www.ruby-forum.com/t/how-to-alias-method-chain-initialize-from-a-mixin/179327 "2009-11-22T08:09:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![evansenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@evansenter](https://www.ruby-forum.com/u/evansenter)\
**Post date:** [November 22, 2009, 8:09am UTC](https://www.ruby-forum.com/t/how-to-alias-method-chain-initialize-from-a-mixin/179327/1 "2009-11-22T08:09:24Z")

</div>

Hi,

I’ve been having a little trouble the past couple days getting an  
alias\_method\_chain working on the initialize method via a module getting  
mixed into a class. For example:

* * *

module Dsl  
def self.included(base)  
base.instance\_eval do  
include InstanceMethods

```
  alias_method :initialize_without_block_support, :initialize
  alias_method :initialize, :initialize_with_block_support
end

```

end

module InstanceMethods  
def initialize\_with\_block\_support  
p “initialize\_with\_block\_support”  
initialize\_without\_block\_support  
end  
end  
end

class Something  
include Dsl

def initialize  
p “initialize\_without\_block\_support”  
end  
end

Something.new #=\> prints “initialize\_without\_block\_support”, should  
print “initialize\_with\_block\_support” as well.

* * *

The problem in this contrived example (I believe) is that initialize is  
redefined in the Something class body after the module is mixed in, thus  
negating the alias\_method\_chain that was set up previously. So one  
option is to just include modules after the initialize method is  
defined, but that’s not at all elegant, so I’ve been trying to find a  
better way.

The next possibility I considered was using the method\_added hook.  
Something that would work like the following:

* * *

module Dsl  
def self.included(base)  
base.extend ClassMethods  
base.instance\_eval { include InstanceMethods }  
end

module ClassMethods  
def method\_added(name)  
if name == :initialize && !@redefining\_initialize  
@redefining\_initialize = :stop\_those\_infinite\_loops!

```
    alias_method :initialize_without_block_support, :initialize
    alias_method :initialize, :initialize_with_block_support

    remove_instance_variable :@redefining_initialize
  end
end

```

end

module InstanceMethods  
def initialize\_with\_block\_support  
p “initialize\_with\_block\_support”  
initialize\_without\_block\_support  
end  
end  
end

class Something  
include Dsl

def initialize  
p “initialize\_without\_block\_support”  
end  
end

Something.new

* * *

This works, but feels pretty hackety. If you happen to have any  
experience doing something like this and have an alternative solution,  
or feedback on this implementation using method\_added, I’d appreciate  
hearing it. As a note, the @redefining\_initialize guard is necessary  
because alias\_method triggers the method\_added hook.

Thanks for your time!

---

<div class="post-metadata">

**Author:** ![evansenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@evansenter](https://www.ruby-forum.com/u/evansenter)\
**Post date:** [November 24, 2009, 12:27am UTC](https://www.ruby-forum.com/t/how-to-alias-method-chain-initialize-from-a-mixin/179327/2 "2009-11-24T00:27:34Z")

</div>

On Nov 21, 2009, at 23:09, Evan Senter wrote:

> Hi,
> 
> I’ve been having a little trouble the past couple days getting an  
> alias\_method\_chain working on the initialize method via a module  
> getting  
> mixed into a class. For example:

Don’t use alias\_method\_chain.

Ever.

You already have inheritance.

It works far better.

Use super:

module DSL  
def initialize  
puts “dsl initialize”  
end  
end

class Something  
include DSL

def initialize  
super  
puts “something initialize”  
end  
end

Oh, inheritance also works when you don’t bother to define initialize  
in Something.

---

<div class="post-metadata">

**Author:** ![evansenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e274bd/32.png) [@evansenter](https://www.ruby-forum.com/u/evansenter)\
**Post date:** [November 24, 2009, 1:05am UTC](https://www.ruby-forum.com/t/how-to-alias-method-chain-initialize-from-a-mixin/179327/3 "2009-11-24T01:05:26Z")

</div>

I explored your idea of using inheritance a bit further, and though it’s  
not entirely transparent to the consuming class (super has to be called)  
it is a clean implementation, and would look something like this:

class A  
def initialize(&block)  
@\_original\_self = block.binding.eval(“self”)  
instance\_eval(&block) if block\_given?  
remove\_instance\_variable :@\_original\_self  
finish\_init if defined? :finish\_init  
end

def print\_me\_a  
p “print\_me\_a”  
end

def method\_missing(name, \*args, &block)  
@\_original\_self ? @\_original\_self.send(name, \*args, &block) : super  
end  
end

class B \< A  
def initialize  
super  
end

def finish\_init  
p “finish\_init”  
end

def print\_me\_b  
p “print\_me\_b”  
end  
end

class C  
def self.run  
B.new do  
print\_me\_a  
print\_me\_b  
print\_me\_c  
end  
end

def self.print\_me\_c  
p “print\_me\_c”  
end  
end

b = C.run

#=\> “print\_me\_a”  
#=\> “print\_me\_b”  
#=\> “print\_me\_c”  
#=\> “finish\_init”

Thanks for the input, it was helpful!
