Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > prior method definition

Reply
Thread Tools

prior method definition

 
 
Mauricio Fernández
Guest
Posts: n/a
 
      04-17-2004

This feels cleaner than the alias_method + redef. method trick, and I
haven't found anything similar in the archives.

It still suffers from the problem exposed in [ruby-talk:75334]. In
addition, blocks cannot be propagated but it will be possible once
[ruby-talk:90671] is implemented (of course, by then we'll have real
advices, but still

batsman@tux-chan:~/src$ cat aop2.rb

class Module
def def_advice(meth, &block)
prev = self.instance_method(meth)
define_method(meth) do |*a|
block.call prev.bind(self), *a
end
end

def def_preadvice(meth, &block)
prev = self.instance_method(meth)
define_method(meth) do |*a|
block.call(*a)
prev.bind(self).call(*a)
end
end

def def_postadvice(meth, &block)
prev = self.instance_method(meth)
define_method(meth) do |*a|
prev.bind(self).call(*a)
block.call(*a)
end
end
end

class A
def foo(*a)
puts "A#foo #{a.inspect}"
end
end
a = A.new
a.foo

class A
def_advice(:foo) do |prev, *a|
puts "before"
prev[*a]
puts "after"
end
end

puts "--"
a.foo

class A
def_preadvice(:foo) { puts "pre 1" }
def_postadvice(:foo) { puts "post 1"}
def_preadvice(:foo) { puts "pre 2" }
end

puts "--"
a.foo
batsman@tux-chan:~/src$ ruby ao
aop.rb aop2.rb
batsman@tux-chan:~/src$ ruby aop2.rb
A#foo []
--
before
A#foo []
after
--
pre 2
pre 1
before
A#foo []
after
post 1


--
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Beeping is cute, if you are in the office
-- Alan Cox


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
Automagic determination of definition based on definition location. Jon Slaughter C++ 4 10-26-2005 05:00 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
aliasing an external method from within a method definition? Francis Hwang Ruby 4 03-08-2005 08:45 AM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments