From: Denis Leclair Date: 2012-08-19T00:10:43+09:00 Subject: accessing protected or private methods from class methods There is all kinds of information out there on Ruby method visibility rules. After doing a lot of reading I think I have pretty good handle on how the rules work but what I'm still struggling with is the philosophy. Let me clarify where I'm having difficulty with a specific example... There are times when I want to setup a class interface in such a way that the normal instantiation method (new() method in most languages) cannot be used and to force the caller to use a class (a.k.a. static) factory method so that certain decisions can be made prior to object creation. After a lot of trial and error, and reading on the subject, I've reached the conclusion that this pattern cannot be done cleanly in Ruby. The fundamental problem seems to be that as soon as the new() method is made private then not only is it restricted from the public API, but it's also blocked from being called from the class' own factory method. The only way I can see to circumvent the restriction is to use the ugly .send() method to bypass visibility rules. The code snippet (http://snipt.org/vAk4) shows how I've implemented this in Ruby, but again, it just seems wrong that I have to bypass the visibility rules using the .send() method. Can anybody clarify the rationale for the restrictions placed on class methods and/or if there is a better way to implement this common pattern in Ruby? Thanks, Denis -- Posted via http://www.ruby-forum.com/.