[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Call a method before a method is called?

Ben Johnson

2/10/2007 7:17:00 PM

Does ruby have "hooks". Where I can define, let's say, a method called
__before that will be called before ANY method call for that class?

Thanks for your help.

--
Posted via http://www.ruby-....

3 Answers

Rodrigo Bermejo

2/10/2007 8:42:00 PM

0

You can do it easily this way (depending what you really want):


def lady
puts "ladies go first"
end

class Myclass
lady
def initialize
end
def man
puts "you look better from here"
end
end


myclass=Myclass.new
myclass.man

/--> ladies go first
/--> you look better from here


A not OO solution is to use:

(from programming ruby book)

Every Ruby source file can declare blocks of code to be run as the file
is being loaded (the BEGIN blocks) and after the program has finished
executing (the END blocks).

BEGIN {
begin code
}

END {
end code
}


A program may include multiple BEGIN and END blocks. BEGIN blocks are
executed in the order they are encountered. END blocks are executed in
reverse order.




hope it helps

-rb.



--
Posted via http://www.ruby-....

Marcello Barnaba

2/10/2007 9:06:00 PM

0

Hi,

On Saturday 10 February 2007 20:17, Ben Johnson wrote:
> Does ruby have "hooks". Where I can define, let's say, a method called
> __before that will be called before ANY method call for that class?

maybe this decorator will fit:

require 'blank' # http://pastie.cabo...

module Decorator
def self.decorate real_class, &block
slate = Blank()
slate.class_eval do
define_method(:real_class) { real_class }
def initialize
@_object = real_class.new
end
def method_missing meth, *args, &block
@_object.send meth, *args, &block
end
end
slate.class_eval &block
slate
end
end

module Kernel
def Decorate klass, &block
Decorator.decorate klass, &block
end
end

class A
def a; 'a' end
end

with_before_hook = Proc.new {
protected
def before_hook meth, *args, &block
STDOUT.puts "before #{meth}: " << "%s %s" % [args.inspect, block.inspect]
end
def method_missing meth, *args, &block
before_hook meth, *args, &block
@_object.send meth, *args, &block
end
}

B = Decorate A, &with_before_hook

>> a = B.new
before inspect: [] nil
=> #<A:0xb7b8b05c>

>> a.class
before class: [] nil
=> A

>> a.methods
before methods: [] nil
=> ["enum_for", "inspect", "send", "taguri.....


but maybe this module is too general for this specific purpouse, but it can
also be adapted by adding singleton class methods like self.before() and
self.after() in the initialize, and arrange them to automatically replace
method_missing with an "hooked" one. or hooks can be explicitely coded into
the main method_missing.. you choose.

:)
--
pub 1024D/8D2787EF 723C 7CA3 3C19 2ACE 6E20 9CC1 9956 EB3C 8D27 87EF

Marcello Barnaba

2/10/2007 9:42:00 PM

0

correction http://pastie.cabo... ..

--
pub 1024D/8D2787EF 723C 7CA3 3C19 2ACE 6E20 9CC1 9956 EB3C 8D27 87EF