Call a method before a method is called?

Discussion in 'Ruby' started by Ben Johnson, Feb 10, 2007.

  1. Ben Johnson

    Ben Johnson Guest

    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/.
     
    Ben Johnson, Feb 10, 2007
    #1
    1. Advertising

  2. 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/.
     
    Rodrigo Bermejo, Feb 10, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    560
    velthuijsen
    Apr 16, 2004
  2. Weng Tianxiang
    Replies:
    6
    Views:
    605
    glen herrmannsfeldt
    Sep 12, 2007
  3. S_K
    Replies:
    6
    Views:
    1,252
    Robert Dunlop
    Nov 8, 2007
  4. Jeff
    Replies:
    0
    Views:
    1,978
  5. Replies:
    10
    Views:
    205
    matt neuburg
    Oct 27, 2006
Loading...

Share This Page