nuby: determine method passed and determine the receiver that received the method

Discussion in 'Ruby' started by Peña, Botp, Jan 24, 2004.

  1. Peña, Botp

    Peña, Botp Guest

    Hi Friends,

    Forgive me in adv for asking a newbie question.

    1. First question: How do I get the the method passed?

    Eg.

    >cat test1.rb

    #-------------------------
    class Dog
    def bark
    'arf!arf!'
    end
    def method_missing(x)
    p "method missing"
    "sorry, cannot do #{x} in #{self}"
    end
    end

    beethoven = Dog.new
    p beethoven.bark
    p beethoven.purr
    #-------------------------

    >ruby -w test1.rb

    "arf!arf!"
    "sorry, cannot do purr in #<Dog:0x2777228>"


    I'd like a method similar to method_missing(x) (wherein the methodname is
    passed in x), but only access it before a method is run (maybe name it
    method_called(x)).

    So I can do (maybe),

    ...
    Class Dog
    ...
    def method_called(x)
    p "You ask for #{x}"
    end
    end


    ------------------
    >ruby -w test1.rb

    "You ask for bark"
    "arf!arf!"
    "You ask for purr"
    "sorry, cannot do purr in #<Dog:0x2777228>"


    2. 2nd question: How do I get the receiver that received the method passed?

    So that:

    >ruby -w test1.rb

    "You ask for bark thru beethoven"
    "arf!arf!"
    "You ask for purr thru beethoven"
    "sorry, cannot do purr thru beethoven"


    That is all ..for the moment. Sorry for the lengthy post.

    thank you in advance.

    kind regards -botp
    Peña, Botp, Jan 24, 2004
    #1
    1. Advertising

  2. Re: determine method passed and determine the receiver that received the method

    "Peña, Botp" <> schrieb im Newsbeitrag
    news:-phil.com...
    > Hi Friends,
    >
    > Forgive me in adv for asking a newbie question.
    >
    > 1. First question: How do I get the the method passed?
    >
    > Eg.
    >
    > >cat test1.rb

    > #-------------------------
    > class Dog
    > def bark
    > 'arf!arf!'
    > end
    > def method_missing(x)
    > p "method missing"
    > "sorry, cannot do #{x} in #{self}"
    > end
    > end
    >
    > beethoven = Dog.new
    > p beethoven.bark
    > p beethoven.purr
    > #-------------------------
    >
    > >ruby -w test1.rb

    > "arf!arf!"
    > "sorry, cannot do purr in #<Dog:0x2777228>"
    >
    >
    > I'd like a method similar to method_missing(x) (wherein the methodname is
    > passed in x), but only access it before a method is run (maybe name it
    > method_called(x)).
    >
    > So I can do (maybe),
    >
    > ..
    > Class Dog
    > ..
    > def method_called(x)
    > p "You ask for #{x}"
    > end
    > end


    Tracing is one option. See
    http://www.rubycentral.com/book/ref_m_kernel.html#Kernel.set_trace_func

    Another option is to use a similar scheme as in Delegator and automatically
    generate proxy methods that invoke your "method_called" just before they
    hand over control to the real method.
    http://www.rubycentral.com/book/lib_patterns.html

    Which of the approaches you choose depends on your situation.

    Tehre might be another option to define a module / class method that
    automatically creates a new method that first invokes method_called and the
    the original method. Sorryy, I don't have the time at the moment to put
    such a thing together.

    As an additional information: there is a feature in the makes that allows to
    define before, after and around hooks for method invocations which would
    enable you to do similar things.

    Regards

    robert
    Robert Klemme, Jan 24, 2004
    #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. dee
    Replies:
    9
    Views:
    505
    Joseph Byrns
    Apr 15, 2005
  2. S
    Replies:
    22
    Views:
    1,642
    Roedy Green
    Dec 9, 2005
  3. Anand
    Replies:
    2
    Views:
    898
    Anand
    Sep 11, 2003
  4. Lilith
    Replies:
    10
    Views:
    491
    peter koch
    Dec 20, 2007
  5. Alfonso
    Replies:
    0
    Views:
    93
    Alfonso
    Jan 17, 2007
Loading...

Share This Page