Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > nuby: determine method passed and determine the receiver that received the method

Reply
Thread Tools

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

 
 
Peņa, Botp
Guest
Posts: n/a
 
      01-24-2004
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






 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      01-24-2004

"Peņa, Botp" <(E-Mail Removed)> schrieb im Newsbeitrag
news:1030AB5FAD0A3847B7B1F7B93332AFB501E18290@bgma il00.delmonte-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_...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

 
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
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Passed value not being received Lilith C++ 10 12-20-2007 05:57 PM
took 272 on 1-6 and passed still have not received anything from MS Ren Hoek MCDST 2 01-23-2006 04:32 AM
Eclipse - access by method name requires a receiver S Java 22 12-09-2005 02:02 AM



Advertisments