From: Ryan Leavengood Date: 2001-10-27T03:04:13+09:00 Subject: [ruby-talk:23483] RE: Capture undefined method calls? I believe the method you want is method_missing: $ ri method_missing -------------------------------------------------- Object#method_missing obj.method_missing( aSymbol [, *args] ) -> anObject ------------------------------------------------------------------------ Invoked by Ruby when obj is sent a message it cannot handle. aSymbol is the symbol for the method called, and args are any arguments that were passed to it. The example below creates a class Roman, which responds to methods with names consisting of roman numerals, returning the corresponding integer values. class Roman def romanToInt(str) # ... end def method_missing(methId) str = methId.id2name romanToInt(str) end end r = Roman.new r.iv #=> 4 r.xxiii #=> 23 r.mm #=> 2000 Ryan Leavengood (finally back on the mailing list) > -----Original Message----- > From: Morris, Chris [mailto:chris.morris@snelling.com] > Sent: Friday, October 26, 2001 1:57 PM > To: ruby-talk ML > Subject: [ruby-talk:23481] Capture undefined method calls? > > I've got the following line: > > document.form.ObjectID.value = 'x' > > form currently is an html dom node. What I'd like to do is something like > (psuedo code falling out all over the place): > > def form > # return a wrapper for the node > NodeWrapper.new(document.form) > end > > class NodeWrapper > def initialize(node) > @node = node > end > > def someSortOfCatchAllForUndefinedCalls(*args) > propName = nameOfUndefinedMethod # e.g. ObjectID > if @node.hasProperty(propName) > @node.propName.value = args[0] > else > assert(false, 'property does not exist') > end > end > end > > then I could simplify the initial call to: > > form.ObjectID = 'x' # maybe, haven't accounted for = sign > > ... and get some additional testing code. Hmmm, well I'm not sure if this > would be that useful to me now that I've thought it through, but I'm still > curious if Ruby can do this sort of thing ... it seems like it could. > > Chris