From: Jean-Hugues ROBERT Date: 2004-06-15T05:17:25+09:00 Subject: Re: method_missing and send At 20:13 14/06/2004 +0900, you wrote: >I'm in the process of writing a logging package for a project. The actual >logging methods can be specified at runtime and are implemented with a class >method_missing call. > >That works fine until I call the warn method using 'send'. I would have >expected the object to respond identically to both the direct invocation and >the indirect invocation via 'send'. > >Is this a bug in ruby or have I missed something? > >class B > def self.method_missing(sym, *args) > puts "%s: %s"%[sym, args[1]] > end >end > >B.log(self, "There was a young man called McKnight,") >B.send(:log, self, "Who could travel faster than light.") >B.warn(self, "One day at about noon, he went to the moon") >B.send(:warn, self, "And arrived back the previous night.") > >bash-2.05b$ ruby -v >ruby 1.8.1 (2004-01-27) [i386-mswin32] >bash-2.05b$ >bash-2.05b$ tmp.rb >log: There was a young man called McKnight, >log: Who could travel faster than light. >warn: One day at about noon, he went to the moon >../tmp.rb:71:in `warn': wrong number of arguments(2 for 1) (ArgumentError) > from ./tmp.rb:71:in `send' > from ./tmp.rb:71 >bash-2.05b$ > > >Neil. Hi, As a side note, you may want to consider a solution using blocks: def trace( domain = :debug, &block ) if $domains[domain] then p block.call() end end trace{ "debug"} trace( :info){ "Hello"} trace( :warn){ "fast" } Doing so is rather efficient when the said domain is disabled. As a result you can keep traces in production code. You can even: def off( domain ) if domain == :trace then alias trace noop end $domains[domain] = false end def noop( *a ); end ... off( :trace) # turns all tracing off Yours, JeanHuguesRobert ------------------------------------------------------------------------- Web: http://hdl.handle.net/1030.37/1.1 Phone: +33 (0) 4 92 27 74 17