Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Call a method before a method is called?

Reply
Thread Tools

Call a method before a method is called?

 
 
Ben Johnson
Guest
Posts: n/a
 
      02-10-2007
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-forum.com/.

 
Reply With Quote
 
 
 
 
Rodrigo Bermejo
Guest
Posts: n/a
 
      02-10-2007
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-forum.com/.

 
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
Is it possible to call button click event before page load on postback GauravGupta ASP .Net 2 07-01-2007 12:16 AM
Function declared before call but defined after - compilation fails johnbrown105@hotmail.com C++ 11 05-18-2007 05:13 PM
Function call after switch but before first case - behavior? yndygo@gmail.com C++ 9 05-02-2007 10:44 PM
Setting member variables before constructor call syntheticpp@yahoo.com C++ 6 01-16-2006 06:07 PM
Before Sunset & Before Sunrise matt r DVD Video 2 11-25-2004 08:37 AM



Advertisments