stephen.hill / motorola.com (Steve Hill) wrote in message news:<c230c758.0108150708.11d81bf2 / posting.google.com>...
> Hi,
> surely an appropriate way to handle this problem would be using a
> Factory design pattern.

Thank you all who gave me adive.  Yes, I remember I learned the
factory pattern a long ago, but I've forgotten it :)  I think the
other two suggestions are also boiled down to the factory pattern:  To
overload MyClass.new is effectively to make "new" the factory; and to
create a factory class method and to use it instead of the "new"
method is similar to creating a stand-alone factory class.

So my final solution looks something like this:

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

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

and I use this class like this:

   while m = MyClass.factory(file)
      arr.push(m)
   end

I'm happy with this solution.

....except for this problem: How should I prevent the user from calling
MyClass.new?  One person (Sorry, but my newsreader doesn't allow me to
go back to the list to check your name until I finish posting.)
suggested that I should make MyClass.new private.  But, I don't know
how to accomplish it.

   class MyClass
      #... methods as above ...
      private
      def MyClass.new
      end
   end

gave me an error

../script.rb:17:in `factory': undefined method `parse' for nil
(NameError)

Could someone help me?  I'm using Ruby 1.4.3 on a Linux box.

Thank you again anyway,
Ryo