From: "David A. Black" Date: 2005-08-30T23:27:48+09:00 Subject: Re: Method behaves differently when called using #send Hi -- On Tue, 30 Aug 2005, Eric Mahurin wrote: > --- James Edward Gray II wrote: > >> On Aug 30, 2005, at 7:53 AM, Eric Mahurin wrote: >> >>> Doing this would make the semantics for calling Object#send >>> different from every other method call. >> >> I disagree completely. It would make send() function THE >> SAME as >> every other method call. That makes a lot of sense to me. >> >> James Edward Gray II > > We are talking about different things. I agree that this > proposal would make obj.send(:meth,...) and send(:meth,...) > function just like obj.meth(...) and meth(...). I'm talking > about about the fact that for all other methods, whether you > have receiver (self) or not doesn't affect the functionality - > only whether the method can be called because of protections. > For this particular method (send) in this proposal, it would > somehow be able to sense whether it was called with a receiver > or not and behave differently. I see what you mean; it's very magic. I can't think of a way to implement it in Ruby, and even though someone will be able to with dl or whatever, it will still be too magic :-) OK, here's another idea -- truly a code-breaker, I should warn you: send(object, method, *args) instead of object.send(method, *args) This would, among other things, make more sense out of the use of "send" as the method name. It would also mean that the privacy issue would not become a matter of syntax, but rather a matter of logic. And it would make send, in practice, behave the same as the dot operator. Here's an implementation and a tiny little test: class UnavailableMethodError < RuntimeError end module Kernel alias :oldsend :send def send(obj,m,*args) if obj.private_methods.include?(m.to_s) raise UnavailableMethodError, "private method `#{m}' called for #{obj.inspect}:#{obj.class}" else obj.oldsend(m, *args) end end end class String def x; end private :x end p send("abc",:split,//) # ["a","b","c"] p send("abc",:x) # error David -- David A. Black dblack@wobblini.net