Avi Bryant <avi / beta4.com> wrote in message news:<Pine.LNX.4.21.0108170330580.27503-100000 / cr798598-a.crdva1.bc.wave.home.com>...
> On Fri, 17 Aug 2001, Ryo Furue wrote:
> 
> > ....except for this problem: How should I prevent the user from calling
> > MyClass.new?
[...]
> does this work?
> 
> class MyClass
>  class << self
>    private :new
>  end
> end

It does, thank you!  Your explanation has made me understand the
"class << obj" thing, which I didn't.  The notation "class << self"
means "let me enter the class to which "self" belongs, that is, the
metaclass of MyClass.  Since we have made "new" a private method of
the metaclass, we can call it only within the same object, MyClass.

As a summary, I show my final (well, I hope this will really be the
final) solution:

   class MyClass
      class <<self
         private :new
      end

      def MyClass.factory(file)
         new.parse(file)
         # The former MyClass.new.parse(file) doesn't work,
         # because you can call a private method
         # only with self as the implicit receiver.
      end

      def parse(file)
         # read file and initialize instance vars.
         successful ? self : nil
      end
   end

You could use protected instead of private.  In that case, you could
say

      def MyClass.factory(file)
         MyClass.new.parse(file)
      end

which may be clearer (to those like me who aren't very much used to
the Ruby language) than the earlier version.

Thank you again,
Ryo