# GOLF (and improve) #instance\_interception

**URL:** <https://www.ruby-forum.com/t/golf-and-improve-instance-interception/98401>\
**Category:** Ruby\
**Created:** [July 14, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/golf-and-improve-instance-interception/98401 "2006-07-14T14:00:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 14, 2006, 2:00pm UTC](https://www.ruby-forum.com/t/golf-and-improve-instance-interception/98401/1 "2006-07-14T14:00:14Z")

</div>

I have been working on this and was hoping maybe others would like to  
take some wacks at it. It’s a general purpose tool for prepending a  
module in the inheritance chain. Here’s an example of it’s use. (code  
follows):

module A  
def f ; “F” ; end  
def g ; “G” ; end

```
instance_interception do
  def f( target, *args, &blk )
    '{' + target.super + '}'
  end
  def g( target, *args, &blk )
    '{' + target.super + '}'
  end
end

```

end

class X  
def f ; super ; end  
include A  
def g ; super ; end  
end

x = X.new  
x.f #=\> “{F}”  
x.g #=\> “#{G}”

# instance\_interception.rb

class Module

def instance\_interception(&block)  
@instance\_interception ||= Module.new do  
def self.append\_features(mod)  
append\_features\_without\_instance\_interception( mod )  
end  
end  
@instance\_interception.module\_eval(&block) if block\_given?  
@instance\_interception  
end

private :instance\_interception

alias\_method :append\_features\_without\_instance\_interception,  
:append\_features

# Append features

def append\_features( mod )

```
aspect = instance_interception
aspect. __send__ ( :append_features_without_instance_interception,

```

mod )

```
aspect.instance_methods.each do |meth|
  if mod.method_defined?( meth )
    aspect.advise( mod, meth )
  end
end

append_features_without_instance_interception( mod )

#if mod.instance_of? Module
aspect. __send__ ( :append_features_without_instance_interception,

```

mod. **send** (:instance\_interception) )  
#end

end

# Apply the around advice.

def advise( mod, meth )  
advice = instance\_method( meth )  
instance\_target = mod.instance\_method(meth)  
mod. **send** ( :define\_method, meth ) { |\*args| #, &blk|  
target = instance\_target.bind( self )  
(class \<\< target; self; end).class\_eval { define\_method( :super  
){ call( \*args ) } }  
advice.bind( self ).call( target, \*args ) #, &blk )  
}  
end

# If a method is added to the module/class that is advised.

def method\_added( meth )  
return if @method\_added\_short  
if instance\_interception.method\_defined?( meth )  
include instance\_interception  
@method\_added\_short = true  
instance\_interception.advise( self, meth )  
@method\_added\_short = false  
end  
end

end

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 14, 2006, 3:16pm UTC](https://www.ruby-forum.com/t/golf-and-improve-instance-interception/98401/2 "2006-07-14T15:16:16Z")

</div>

removed\_email\_address@domain.invalid wrote:

> > def f ; “F” ; end  
> > def g ; “G” ; end
> 
> Well, the golf part is easy, but I don’t think it looks very good:
> 
> module A;def f;“F”;end;def g;“G”;end;end
> 
> Are you sure you want to golf this? 🙂

Well, okay. I guess golf isn’t exactly what I mean. Just \*improve"  
then. Thanks.

BTW, typo in example:

x.g #=\> “#{G}”

should be

x.g #=\> “{G}”

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 14, 2006, 3:06pm UTC](https://www.ruby-forum.com/t/golf-and-improve-instance-interception/98401/3 "2006-07-14T15:06:48Z")

</div>

Hi –

On Fri, 14 Jul 2006, removed\_email\_address@domain.invalid wrote:

> I have been working on this and was hoping maybe others would like to  
> take some wacks at it. It’s a general purpose tool for prepending a  
> module in the inheritance chain. Here’s an example of it’s use. (code  
> follows):
> 
> module A  
> def f ; “F” ; end  
> def g ; “G” ; end

Well, the golf part is easy, but I don’t think it looks very good:

module A;def f;“F”;end;def g;“G”;end;end

Are you sure you want to golf this? 🙂

David

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2006, 5:25am UTC](https://www.ruby-forum.com/t/golf-and-improve-instance-interception/98401/4 "2006-07-15T05:25:18Z")

</div>

Ryan D. wrote:

> It looks an awful lot like MuffDaddy to me.
> 
> [http://blog.zenspider.com/archives/2005/02/muffdaddy\_the\_u.html](http://blog.zenspider.com/archives/2005/02/muffdaddy_the_u.html)

'Fraid MuffDaddy’z not so “ultimate” – It’s not dynamic.

T.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 15, 2006, 3:00am UTC](https://www.ruby-forum.com/t/golf-and-improve-instance-interception/98401/5 "2006-07-15T03:00:27Z")

</div>

It looks an awful lot like MuffDaddy to me.

[http://blog.zenspider.com/archives/2005/02/muffdaddy\_the\_u.html](http://blog.zenspider.com/archives/2005/02/muffdaddy_the_u.html)
