From: Michael Neumann Date: 2004-12-30T20:58:01+09:00 Subject: Re: #send and private methods Marc Merlin wrote: > [...] > In other words, information hiding is a way of making software systems > resilient in the face of change. It is not simply a way "nanny" > programming languages try to keep "top-gun" programmers from getting their > job done. Of course, for small projects with a restricted user base these > constraints hardly matter. If Ruby were intended to be only a prototyping > tool, then bypassing access controls would be no big deal. But Ruby > apparently is on the way to being adopted as a bona fide > progamming-language-in-the-large, as evidenced by such trailblazing > projects as the Rails framework. (I certainly hope so!) Consequently I > think that the adherence to a rigorous informtation hiding schema should be > taken seriously and mechanisms to bypass it should be brought into > question. I think #send should not be able to call private methods (from a proper OO perspective, not neccessarily mine ;-), but it should be able to call protected methods. Nevertheless, I think deciding whether a method should be declared as protected or private is not always easy, and I would not matter if there were only "protected" methods in Ruby (which means that they have to be called with "self" as receiver, regardless of the class they were defined). Of course there are for sure many situations where you'd want to have private methods. What you want is probably this: class Object def send(id, *args, &block) if private_methods.include?(id.to_s) raise "private method `#{ id } called for #{ self }" end __send__(id, *args, &block) end end That's the good thing with Ruby. Without the current behaviour of __send__ (or send), you could not implement what currently is possible (call private methods). Regards, Michael